More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter...

19
More Design Patterns Horstmann ch.10.1,10.4

Transcript of More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter...

Page 1: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

More Design Patterns

Horstmann ch.10.1,10.4

Page 2: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

Design patterns

• Structural design patterns– Adapter– Composite– Decorator– Proxy

• Behavioral design patterns– Iterator– Observer– Strategy– Template method– Visitor

Page 3: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 4: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

Adapter Have Icon Want Component

Page 5: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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()); }}

Page 6: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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. 

Page 7: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

The ADAPTER Pattern

Page 8: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

The ADAPTER Pattern

Icon

paintComponent()

JComponent

paintIcon()

JFrame

IconAdapter

Page 9: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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");

Page 10: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

The ADAPTER Pattern

InputStream

read()

Reader

read()

InputStreamReader

Page 11: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 12: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 13: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 14: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 15: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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.   

Page 16: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

The PROXY Pattern

Page 17: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

The PROXY Pattern

Icon

ImageIconImageProxy

paintIcon()

paintIcon()

JLabel

Page 18: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Page 19: More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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