Design Pattern Guarded Suspension - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Design Pattern Guarded Suspension

Description:

Container getContentPane() dispose() // from Window. pack() // from Window ... each container has a layout manager. handles the layout of the components ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 28
Provided by: michael742
Category:

less

Transcript and Presenter's Notes

Title: Design Pattern Guarded Suspension


1
Design Pattern Guarded Suspension
  • a concurrency pattern
  • intent
  • avoid the deadlock situation that can occur when
    a thread is about to execute an objects
    synchronized method and the state of the object
    prevents the method from executing to completion
  • motivation
  • a Queue class in a multiple thread environment
  • call of pull when the Queue is empty (caller has
    to wait until an element is pushed to the queue)
  • solution
  • if the precondition is not fulfilled wait but
    release the lock (in Java use wait())

2
Collaboration diagram
1a pull() concurrency guarded !isEmpty()
1b push() concurrency guarded
q Queue
3
Example
  • public class QueueltEgt
  • private ArrayListltEgt data new ArrayListltEgt()
  • synchronized public void push(E element)
  • data.add(element)
  • notify()
  • synchronized public E pull()
  • while (data.size() 0)
  • try wait() catch (Exception e)
  • E element data.removeFirst(0)
  • return element

4
Design Patter Producer-Consumer
  • aka Pipe (just one Producer)
  • a concurrency pattern
  • intent
  • coordination of asynchronous production and
    consumption of information or objects

Queue-produced objects
1
0..
Producer
Consumer
0..
consumer
1
queue
Consume-queued objects
5
Example
  • Class Queue as before (using the design pattern
    Guarded Suspension).
  • public class Producer implements Runnable
  • private Queue myQueue
  • public Producer(Queue myQueue)
  • this.myQueue myQueue
  • public void run()
  • myQueue.push(myData)

6
Example (contd)
  • public class Consumer implements Runnable
  • private Queue myQueue
  • public Consumer(Queue myQueue)
  • this.myQueue myQueue
  • public void run()
  • myData myQueue.pull()

7
GUI Programming
  • graphical user interface
  • human-computer interface
  • replaces command-line interface
  • interface builders
  • Java AWT/Swing
  • behavior is platform independent
  • event-driven programming
  • user in control
  • user actions ? events
  • program responds to events

8
AWT and Swing
  • AWT components are heavyweight
  • associated with native components created by the
    underlying window system
  • appearance determined by the window system
  • different platforms different look
  • Swing components are lightweight
  • created by the JVM
  • appearance determined by the JVM
  • different platforms same look
  • Composite design pattern

9
AWT classes
Component
Container
Button
Canvas
Panel
Window
Checkbox
ScrollPane
Choice
Label
Frame
Dialog
List
Scrollbar
TextComponent
TextArea
TextField
10
Swing classes
Container
Component
JComponent
JButton
JPanel
Window
JCombobox
JCheckbox
JLabel
Frame
Dialog
JList
JScrollbar
JFrame
JDialog
JTextComponent
JTextArea
JTextField
JWindow
11
Component
  • abstract class
  • methods
  • paint(Graphics g)
  • repaint() calls paint
  • setSize(int width, int height)
  • setVisible(boolean b)
  • requestFocus()
  • validate()

12
Container
  • abstract class
  • widget containers
  • can contain containers
  • layout
  • LayoutManager
  • methods
  • add(Component comp)
  • add(String name, Component comp)
  • remove(Component comp)
  • removeAll()
  • setLayout(LayoutManager mgr)
  • paint(Graphics g) // from Component
  • should be overridden in order to draw
  • all components of the container

13
JComponent
  • Swing version of Component
  • abstract class
  • adds additional functionality
  • new methods
  • Dimension getPreferredSize()
  • Dimension getSize(Dimension rv)
  • setPreferredSize(Dimension preferredSize)
  • setEnabled(boolean enabled)

14
JFrame
  • top level window
  • components
  • menu bar
  • content pane
  • all layout etc.
  • constructors
  • JFrame()
  • JFrame(String title)
  • methods
  • Container getContentPane()
  • dispose() // from Window
  • pack() // from Window
  • setJMenuBar(JMenuBar menuBar)
  • setResizable(boolean b) // from Frame
  • setTitle(String title) // from Frame
  • setDefaultCloseOperation(int operation)

15
Layout managers
  • each container has a layout manager
  • handles the layout of the components contained in
    the container
  • setLayout(lm) sets lm as the layout manager of
    this container

Container
LayoutManager
BorderLayout
CardLayout
FlowLayout
GridLayout
16
Layout managers
  • BorderLayout manager can manage five components,
  • default layout manager for Frame, JFrame
  • locations North, East, South, West, Center
  • add(North, new Jbutton(Button 1)
  • GridLayout manager creates a rectangular array of
    components
  • each component occupies the same size portion of
    the screen
  • new GridLayout(4,4,3,3)
  • FlowLayout manager places components in rows left
    to right, top to bottom
  • components need not to have the same size
  • default manager for Panel
  • CardLayout manager stacks components vertically
  • only one component is visible at one time
  • components can be named
  • CardLayout lm new CardLayout()
  • Panel p new Panel(lm)
  • p.add(One, new Label(Number One))
  • lm.show(p,One)

17
Reaction Model
  • reactive
  • react to event as soon as it happens
  • usually for buttons and menu selections
  • write Listener and handle event
  • used when view must change immediately
  • completed data entry
  • doing input validation
  • reactive selections
  • passive
  • dont react to event when it happens
  • wait until significant event (e.g. button press)
  • access widget to get its state/contents
  • usually for information entry
  • text boxes
  • selections

18
Events
  • caused by user actions
  • event objects
  • represent particular events
  • carry further information
  • event source objects
  • register listeners
  • send listeners events
  • GUI widgets
  • listeners
  • receive/handle events
  • listener interfaces

19
Example - ActionEvent
  • button presses cause ActionEvents
  • ActionListeners handle ActionEvents
  • actionPerformed
  • getActionCommand
  • JButton
  • constructor
  • addActionListener
  • setActionCommand
  • creating a button on the fly
  • JButton button new JButton("Button")
  • button.addActionListener(new ActionListener()
  • public void actionPerformed (ActionEvent e)
  • //do something
  • )

20
Example (cont.)
  • using a private listener class
  • JButton button new JButton("Button")
  • button.addActionListener(new MyActionListener())
  • ?
  • private class MyActionListener implements
    ActionListener public void actionPerformed
    (ActionEvent e)
  • //do something

21
Example (cont.)
  • combining the button and the listener in a new
    class
  • JButton button new MyButton()
  • ?
  • private class MyButton extends JButton
  • implements ActionListener
  • public MyButton()
  • super("Button")
  • addActionListener(this)
  • public void actionPerformed (ActionEvent e)
  • //do something

22
Example (cont.)
  • a button to make a window invisible
  • private class MyOKButton extends JButton
  • implements ActionListener
  • private JDialog parent
  • public MyOKButton(JDialog parent)
  • super("Ok")
  • this.parent parent
  • addActionListener(this)
  • public void actionPerformed (ActionEvent e)
  • parent.setVisible(false)

23
Example (cont.)
  • generic ButtonAdapter class
  • abstract class ButtonAdapter extends JButton
  • implements ActionListener
  • public ButtonAdapter(String name)
  • super(name)
  • addActionListener(this)
  • public void actionPerformed(ActionEvent e)
  • pressed()
  • public abstract void pressed()

24
Listeners / Events /Adapters
  • listener classes
  • ActionListener
  • ItemListener
  • ListSelectionListener
  • AdjustmentListener
  • KeyListener
  • MouseListener
  • MouseWheelListener
  • WindowListener
  • ComponentListener
  • ContainerListener
  • event classes
  • ActionEvent
  • ItemEvent
  • ListSelectionEvent
  • AdjustmentEvent
  • KeyEvent
  • MouseEvent
  • MouseWheelEvent
  • WindowEvent
  • ComponentEvent
  • ContainerEvent
  • adapter classes
  • KeyAdapter
  • MouseAdapter
  • MouseMotionAdapter
  • MouseInputAdapter
  • WindowAdapter
  • ComponentAdapter
  • ContainerAdapter

25
Event Hierarchy
  • semantic events
  • ActionEvent
  • button click (incl. checkbox and radio button),
    menu selection combo box selection, enter in text
    area
  • ItemEvent
  • change of the state of an item, e.g., checkbox
    and radio box
  • ListSelectionEvent
  • list selections
  • AdjustmentEvent
  • scroll bar changes
  • low level events
  • KeyEvent
  • MouseEvent
  • MouseWheelEvent
  • WindowEvent

26
Listeners (cont.)
  • handling window closing
  • JFrame
  • top level window
  • window events
  • WindowListener
  • windowClosing
  • addWindowListener(new WindowAdapter()
  • public void windowClosing(WindowEvent e)
  • setVisible(false)
  • dispose()
  • System.exit(0)
  • )
  • WindowAdapter
  • null methods

27
Listeners (cont.)
  • private class MyMouseListener extends
    MouseAdapter
  • public void mouseClicked (MouseEvent me)
  • int x me.getX()
  • int y me.getY()
  • if (me.getButton() MouseEvent.BUTTON1)
  • // action for left mouse button
  • if (me.getButton() MouseEvent.BUTTON2)
  • // action for middle mouse button
  • if (me.getButton() MouseEvent.BUTTON3)
  • // action for right mouse button
Write a Comment
User Comments (0)
About PowerShow.com