Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators...
Transcript of Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators...
![Page 1: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/1.jpg)
Patterns & GUI Programming
Part 2
1
![Page 2: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/2.jpg)
Creating a Custom Layout Manager
• Layout manager determines how components are arranged/displayed in a container
• Can determine your own layout pattern by implementing the LayoutManager interface
2
![Page 3: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/3.jpg)
LayoutManager interface
3
public interface LayoutManager
{
void layoutContainer(Container parent);
Dimension minimumLayoutSize(Container parent);
Dimension preferredLayoutSize(Container parent);
void addLayoutComponent(String name, Component comp);
void removeLayoutComponent(Component comp);
}
![Page 4: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/4.jpg)
LayoutManager methods
• minimumLayoutSize() and preferredLayoutSize() determine the minimum and default size of the container when components are laid out
• Start with preferredLayoutSize - determine the height and width of each component, and use the combined sizes to determine how large window must be
• Can implement minimumLayoutSize() by just having it call preferredLayoutSize()
4
![Page 5: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/5.jpg)
LayoutManager methods
• layoutContainer() lays out components by setting position and size of each
• Method computes positions of each component then calls setBounds to put each component in its correct location
5
![Page 6: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/6.jpg)
Strategy Pattern
• Context:
– A class can benefit from different variants for an algorithm
– Clients sometimes want to replace standard algorithms with custom versions
6
![Page 7: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/7.jpg)
Strategy Pattern
• Solution
– Define an interface type that is an abstraction for the algorithm
– Actual strategy classes realize this interface type.
– Clients can supply strategy objects
– Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object
7
![Page 8: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/8.jpg)
Strategy Pattern
8
![Page 9: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/9.jpg)
Strategy Pattern - examples
• Pluggable strategy for layout management
– Layout manager object responsible for executing concrete strategy
– Generalizes to Strategy Design Pattern
• Other manifestation: ComparatorsComparator comp = new CountryComparatorByName();
Collections.sort(countries, comp);
9
![Page 10: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/10.jpg)
Layout Management - Strategy
• Name in Pattern
– Context
– Strategy
– ConcreteStrategy
– doWork()
• Actual name
Container
LayoutManager
BorderLayout, FlowLayout, GridLayout, etc.
layoutContainer()
10
![Page 11: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/11.jpg)
Sorting - Strategy
• Design Name
– Context
– Strategy
– ConcreteStrategy
– doWork()
• Actual name
Collections
Comparator
Comparator-implementing class
compare()
11
![Page 12: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/12.jpg)
Composite Pattern
• Context
– Primitive objects can be combined to composite objects
– Clients treat a composite object as a primitive object
12
![Page 13: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/13.jpg)
Composite Pattern
• Solution
– Define an interface type that is an abstraction for the primitive objects
– Composite object collects primitive objects
– Composite and primitive classes implement same interface type.
– When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results
13
![Page 14: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/14.jpg)
Composite Pattern
14
![Page 15: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/15.jpg)
Containers and Components
• Containers collect GUI components
– Sometimes, want to add a container to another container
– Container should be a component
• Composite design pattern
– Composite methods typically invoke component methods
– E.g. Container.getPreferredSize invokes getPreferredSize of components
15
![Page 16: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/16.jpg)
AWT components - composite
• Design name
– Primitive
– Composite
– Leaf
– method()
• Actual name
Component
Container
JButton, JPanel, JOptionPane, or other class with no children
Component method such as getPreferredSize()
16
![Page 17: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/17.jpg)
Decorator Pattern
• Context
– Component objects can be decorated (visually or behaviorally enhanced)
– The decorated object can be used in the same way as the undecorated object
– The component class does not want to take on the responsibility of the decoration
– There may be an open-ended set of possible decorations
17
![Page 18: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/18.jpg)
Decorator Pattern
• Solution
– Define an interface type that is an abstraction for the component
– Concrete component classes realize this interface type.
– Decorator classes also realize this interface type.
18
![Page 19: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/19.jpg)
Decorator Pattern
• Solution– A decorator object manages the component
object that it decorates
– When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.
19
![Page 20: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/20.jpg)
Decorator Pattern
20
![Page 21: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/21.jpg)
Scroll Panes & Scroll Bars
• Scroll bars useful when a component contains more information than can be displayed in available space
• Add functionality to underlying component
• JScrollPane decorates a component and is itself a component
21
![Page 22: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/22.jpg)
JScrollPane
Scroll bars can be attached to components
Approach #1: Component class can turn on scroll bars
Approach #2: Scroll bars can surround component
JScrollPane pane = new JScrollPane(component);
Swing uses approach #2 - JScrollPane is again a component
22
![Page 23: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/23.jpg)
Decorator Pattern: Scroll Bars
• Design name:
– Component
– ConcreteComponent
– Decorator
– method()
• Actual name:
Component
JTextArea
JScrollPane
paint() or other method of Component
23
![Page 24: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/24.jpg)
I/O stream filters as decorators
• BufferedReader takes a Reader and adds buffering
• Result is another Reader: Decorator patternInputStreamReader r = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(r);
• Many other decorators in stream library, e.g. PrintWriter
24
![Page 25: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/25.jpg)
Decorator pattern: I/O filters
• Design name:
– Component
– ConcreteComponent
– Decorator
– method()
• Actual name:
Reader
InputStreamReader
BufferedReader
read()
25
![Page 26: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/26.jpg)
How to Recognize Patterns
• Look at intent of pattern: e.g. COMPOSITE has different intent than DECORATOR
• Remember common uses (e.g. STRATEGY for layout managers)
• Use context and solution as "litmus test"
26
![Page 27: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/27.jpg)
Recognizing Patterns
• Not everything that is strategic is an example of STRATEGY pattern
– Context must want to use different variants of an algorithm
– Must be an interface type that is an abstraction of the algorithm; concrete strategy classes must implement the interface
– Client supplies object of strategy class to context class; context class uses strategy object to invoke algorithm
27
![Page 28: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/28.jpg)
Litmus test - example
Can add border to Swing componentBorder b = new EtchedBorder()
component.setBorder(b);
• Undeniably decorative
• Is it an example of DECORATOR?
28
![Page 29: Patterns & GUI Programming - Kirkwood · 2019. 9. 20. · I/O stream filters as decorators •BufferedReader takes a Reader and adds buffering •Result is another Reader: Decorator](https://reader035.fdocuments.us/reader035/viewer/2022071212/6027eab357953e7ba142b556/html5/thumbnails/29.jpg)
Litmus test - example
• Component objects can be decorated (visually or behaviorally enhanced): PASS
• The decorated object can be used in the same way as the undecorated object: PASS
• The component class does not want to take on the responsibility of the decoration: FAIL--the component class has setBorder method
29