design patterns se464 derek rayside images from netobjectives.com & wikipedia

33
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Upload: kristian-booth

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Design Patterns

SE464Derek Rayside

images from NetObjectives.com& Wikipedia

Page 2: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

modes of normal composition

fitness for future

Page 3: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Creational Structural Behavioural

Abstract FactoryBuilder

Factory MethodPrototypeSingleton

AdapterBridge

CompositeDecoratorFacade

FlyweightProxy

Chain of ResponsibilityCommandInterpreter

IteratorMediatorMementoObserver

StateStrategy

Template MethodVisitor

Page 4: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Creational Structural Behavioural

  

Singleton

AdapterBridge

Composite

Facade

CommandInterpreter

Observer

Strategy

Visitor

Page 5: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Creational Structural Behavioural

  

Singleton

AdapterBridge

Facade

Command

Observer

Strategy

Page 6: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Creational Structural Behavioural

  

Singleton

AdapterBridge

Observer

Strategy

Page 7: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Page 8: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Page 9: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Design challenge:  the observers need to know more

Page 10: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Design challenge:  the observers need to know more

Solution 1:  add parameters to notify method

public interface Observer {public notify(String acct, double amt, String    emailAddr, String overdraftAcct);}public Audit implements Observer {public void notify(String auditedAccount,    double overdrawnAmount,    String ignoreEmailAddr,    String ignoreOverdraftAcct) {// write info to log, take other appropriate action}}

Page 11: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Complications:• Need to add new parameters in all existing Listeners• May be sending unused data to Listeners

Page 12: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Design challenge:  the observers need to know more Solution 2: callbacks

Page 13: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Complications:• May reveal too much information to Listeners

o Solution: pass a Msg object that encapsulates the Account information instead of passing Account object by reference

• Each listener will be blocked until previous listeners are done (in single-threaded situations)

• The state of the objects passed to Listeners might change in concurrent applications 

Page 14: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Design challenge:  the observers need to know moreSolution 3: reify the event

Page 15: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Observer (Publish/Subscribe)

Design challenge:  the observers need to know moreSolution 4: new Observer interface

• Very simple• How to distinguish between the old (legacy) interface and

the new Observer interfaceo ex. The new interface extends old interface and adds two

new methods.

Page 16: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Strategy

Vary the algorithm independently of the clients who use it.

Page 17: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Strategy

Vary the algorithm independently of the clients who use it.

Page 18: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Strategy

Vary the algorithm independently of the clients who use it. 1.  Who chooses the strategy?

2.  Are strategy objects mutable? Examples:• Strategy used to sort a list of numbers - if known the list is

almost sorted, use merge sort; otherwise use quicksort

Page 19: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Strategy

Who choosesthe strategy?

a. clientb. contextc. config file    (not pictured)

Page 20: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Strategy

Mutable Strategy Objects• easier to return more

complex results• need to be instantiated for

each use

Stateless Strategy Objects• reusable• re-entrant• simpler API usage rules• can be Singleton

Page 21: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Singleton

Ensure a class has only one instance, and provide a global point of access to it.

Page 22: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Singleton

Advantages• convenience• controlled access• reduced namespace• can substitute alternatives• more flexible than static

methods

Issues•  global variables

o make testing harder•  synchronization•  may reduce parallelism•  memory leaks 

 •  initialization

 •  class-loaders vs VMs•  distributed systems •  may hinder re-use

Page 23: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Singleton:  traditional implementation

public class Singleton {    private static final Singleton INSTANCE = new Singleton();

    // Private constructor prevents external instantiation    private Singleton() {}

    public static Singleton getInstance() {        return INSTANCE;    }}

Page 24: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Singleton: safer initialization [Pugh]

public class Singleton {    // Private constructor prevents external instantiation    private Singleton() {}

    /**     * SingletonHolder is loaded on the first execution      * of Singleton.getInstance() or the first access to      * SingletonHolder.INSTANCE, not before.     */    private static class SingletonHolder {         static final Singleton INSTANCE = new Singleton();    }

    public static Singleton getInstance() {        return SingletonHolder.INSTANCE;    }}

Page 25: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Adapter (Wrapper)

Convert the interface of a class into another interface clients expect.

Page 26: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Adapter

 

Page 27: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Adapter

To consider:1. Large delta between local & foreign => facade– Exceptions?– Instantiation?– Does the adapter need to add functionality?

    example: SWT and Swing.– Stateful? Probably shouldn't be.

Page 28: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Bridge

Decouple an abstraction from its implementation so that the two can vary independently.

Page 29: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Bridge

Decouple an abstraction from its implementation so that the two can vary independently.

Page 30: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Adapter vs Bridge

Adapter• connection unforseen• interfaces already exist

Bridge• connection forseen• need to be able to

subclass both abstraction and implementation

Page 31: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Other patterns you need to know

Lectures so far & Lab1 • visitor• interpreter• iterator• composite

Reading & Future Lecture• facade [cf Bowman]• command

Page 32: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Comprehension Questions

• What are some ways of adapting an existing Observer class when more information is needed by a type of event? What are some pros and cons of these methods?

• Name some benefits and downsides of mutable and stateless Strategy objects.

• What are some issues with the Singleton pattern?• Name some uses of the Strategy, Adapter, Singleton,

Bridge, and Observer patterns. • In GUI Framework, when a button is pressed, an

OnButtonPressed event is received. What type of pattern is being used?o Answer: Observer Design Pattern is used. All the

Observers (or Listeners in Java) are notified.

Page 33: Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

Comprehension Questions

• Why is it generally considered bad practice to have mutable Singleton classes?

• What are some differences between the Adapter and the Bridge design pattern? Give an example of both to support your answer.

• In Java, switching between different layouts using a context makes use of what design pattern? o Answer: Strategy Design Pattern