More Design Patterns Horstmann ch.10.1,10.4 Design patterns - - PDF document

more design patterns
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

More Design Patterns

Horstmann ch.10.1,10.4

slide-2
SLIDE 2

Design patterns

  • Structural design patterns

– Adapter – Composite – Decorator – Proxy

  • Behavioral design patterns

– Iterator – Observer – Strategy – Template method – Visitor

slide-3
SLIDE 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

slide-4
SLIDE 4

Adapter

Have Icon Want Component

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

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

The ADAPTER Pattern

slide-8
SLIDE 8

The ADAPTER Pattern

Icon paintComponent() JComponent paintIcon() JFrame IconAdapter

slide-9
SLIDE 9

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:

slide-10
SLIDE 10

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

slide-11
SLIDE 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

slide-12
SLIDE 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

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

slide-14
SLIDE 14

The PROXY Pattern

slide-15
SLIDE 15

The PROXY Pattern

Icon ImageIcon ImageProxy paintIcon() paintIcon() JLabel

slide-16
SLIDE 16

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