Post on 14-Jan-2016
transcript
More Design Patterns
Horstmann ch.10.1,10.4
Design patterns
• Structural design patterns– Adapter– Composite– Decorator– Proxy
• Behavioral design patterns– Iterator– Observer– Strategy– Template method– Visitor
Adapters
• Cable adapter: adapts plug to foreign wall outlet • OO Programming; Want to adapt class to foreign
interface type • Example: Add CarIcon to container • Problem: Containers take components, not icons • Solution: Create an adapter that adapts Icon to Component
Adapter Have Icon Want Component
Adapter
public class IconAdapter extends JComponent {
private Icon icon;
public IconAdapter(Icon i) { icon = i; }
public void paintComponent(Graphics g) { icon.paintIcon(this, g, 0, 0); }
public Dimension getPreferredSize() { return new Dimension(icon.getIconWidth(), icon.getIconHeight()); }}
The ADAPTER PatternContext• You want to use an existing class (adaptee) without modifying it.
• The context in which you want to use the class requires target interface that is different from that of the adaptee.
• The target interface and the adaptee interface are conceptually related.
Solution• Define an adapter class that implements the target interface.
• The adapter class holds a reference to the adaptee. It translates target methods to adaptee methods.
• The client wraps the adaptee into an adapter class object.
The ADAPTER Pattern
The ADAPTER Pattern
Icon
paintComponent()
JComponent
paintIcon()
JFrame
IconAdapter
The ADAPTER Pattern
• In stream library • Input streams read bytes • Readers read characters • Non-ASCII encoding: multiple bytes per char • System.in is a stream • What if you want to read characters? • Adapt stream to reader • InputStreamReader
Reader r1 = new InputStreamReader(System.in);Reader r2 = new InputStreamReader(System.in,"ISO-8859-1");
The ADAPTER Pattern
InputStream
read()
Reader
read()
InputStreamReader
QUIZ
• We have list, but we want stack. • Can we make adapter?
1. No, it is not possible
2. Yes, it is possible, but UML diagram is wrong
3. Yes, it is possible and UML diagram is correct
4. I don’t know
Proxies• Proxy: a person who is
authorized to act on another persons behalf
• Example: Delay instantiation of object
• Expensive to load image
• Not necessary to load image that user doesn't look at
• Proxy defers loading until user clicks on tab
Image Loading
Simple approach: load image files from web address
JTabbedPane tabbedPane = new JTabbedPane();
for (int i=100; i<200; i++) {
URL url = new URL("http://www.cs.au.dk/dProg2/..."+i+”.jpg”);
JLabel label = new JLabel( new ImageIcon(url) );
tabbedPane.add(i, label);
}
All 100 pictures are loaded before the first is displayed
May be slow May overflow memory
Deferred Image Loading
• Simple image loading: JLabel label = new JLabel( new ImageIcon(url) );
• Using proxy:JLabel label = new JLabel( new ImageProxy(url) );
• paintIcon loads image if not previously loaded
public void paintIcon(Component c, Graphics g, int x, int y) { if (image == null) image = new ImageIcon(url); image.paintIcon(c, g, x, y);}
Picture is fetched only if/when needed
The PROXY PatternContext • A class (the real subject) provides a service that is specified by an
interface type (the subject type)
• There is a need to modify the service in order to make it more versatile.
• Neither the client nor the real subject should be affected by the modification.
Solution • Define a proxy class that implements the subject interface type.
The proxy holds a reference to the real subject, or otherwise knows how to locate it.
• The client uses a proxy object.
• Each proxy method invokes the same method on the real subject and provides the necessary modifications.
The PROXY Pattern
The PROXY Pattern
Icon
ImageIconImageProxy
paintIcon()
paintIcon()
JLabel
QUIZ• We want to offer an undo-last-operation for queues without messing with the
code of existing queue classes. Which pattern is appropriate?
1. Adapter
2. Proxy
3. Decorator
4. Composite
5. None of the above
6. I don’t know
QUIZ
• Which design pattern is used here?
1. Adapter
2. Composite
3. Decorator
4. Proxy
5. None of the above
6. I don’t know