more design patterns
play

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


  1. More Design Patterns Horstmann ch.10.1,10.4

  2. Design patterns • Structural design patterns – Adapter – Composite – Decorator – Proxy • Behavioral design patterns – Iterator – Observer – Strategy – Template method – Visitor

  3. 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

  4. Have Icon Want Component Adapter

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

  6. 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.

  7. The ADAPTER Pattern

  8. The ADAPTER Pattern JComponent paintComponent() JFrame Icon IconAdapter paintIcon()

  9. QUIZ Adapter pattern Stack: 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

  10. 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

  11. 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

  12. 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

  13. 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.

  14. The PROXY Pattern

  15. The PROXY Pattern JLabel Icon paintIcon() ImageProxy ImageIcon paintIcon()

  16. QUIZ Which pattern? 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 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; } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend