More Design Patterns Horstmann ch.10.1,10.4 Design patterns - - PDF document
More Design Patterns Horstmann ch.10.1,10.4 Design patterns - - PDF document
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
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 Pattern
Context
- 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
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
Adapter pattern Stack:
Proxies
- Proxy: a person who is
authorized to act on another persons behalf
- Example: Delay instantiation
- f 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 Pattern
Context
- 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 ImageIcon ImageProxy paintIcon() paintIcon() JLabel
QUIZ
Only super-users should be able to delete items from the FilmCatalogue. Which pattern does the code and the UML diagram exemplify?
- 1. Adapter
- 2. Proxy
- 3. Composite
- 4. None of the above
- 5. I don’t know
Which pattern?
public interface Catalogue<E> { public boolean add(E item); public boolean delete(E item); } public class SecureCatalogue<E> implements Catalogue<E> { private boolean superUser; private Catalogue<E> cat = new FilmCatalogue<E>();; public SecureCatalogue(boolean su) { superUser = su; } public boolean add(E item) { return cat.add(item); } public boolean delete(E item) { if (superUser) return cat.delete(item); else return false; } }