Design Patterns SE464 Derek Rayside images from NetObjectives.com - - PowerPoint PPT Presentation
Design Patterns SE464 Derek Rayside images from NetObjectives.com - - PowerPoint PPT Presentation
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia modes of normal composition fitness for future Creational Structural Behavioural Abstract Factory Adapter Chain of Responsibility Builder Bridge Command
modes of normal composition fitness for future
Creational Structural Behavioural Abstract Factory Builder Factory Method Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor
Creational Structural Behavioural Singleton Adapter Bridge Composite Facade Command Interpreter Observer Strategy Visitor
Creational Structural Behavioural Singleton Adapter Bridge Facade Command Observer Strategy
Creational Structural Behavioural Singleton Adapter Bridge Observer Strategy
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.
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.
Observer (Publish/Subscribe)
Design challenge: the observers need to know more
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 } }
Observer (Publish/Subscribe)
Complications: Need to add new parameters in all existing Listeners May be sending unused data to Listeners
Observer (Publish/Subscribe)
Design challenge: the observers need to know more Solution 2: callbacks
Observer (Publish/Subscribe)
Complications: May reveal too much information to Listeners 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
Observer (Publish/Subscribe)
Design challenge: the observers need to know more Solution 3: reify the event
Observer (Publish/Subscribe)
Design challenge: the observers need to know more Solution 4: new Observer interface Very simple How to distinguish between the old (legacy) interface and the new Observer interface
- ex. The new interface extends old interface and adds
two new methods.
Strategy
Vary the algorithm independently of the clients who use it.
Strategy
Vary the algorithm independently of the clients who use it.
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
Strategy
Who chooses the strategy?
- a. client
- b. context
- c. config file
(not pictured)
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
Singleton
Ensure a class has only one instance, and provide a global point of access to it.
Singleton
Advantages convenience controlled access reduced namespace can substitute alternatives more flexible than static methods Issues global variables make testing harder synchronization may reduce parallelism memory leaks initialization class-loaders vs VMs distributed systems may hinder re-use
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; } }
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; } }
Adapter (Wrapper)
Convert the interface of a class into another interface clients expect.
Adapter
Adapter
To consider:
- 1. Large delta between local & foreign => facade
- 2. Exceptions?
- 3. Instantiation?
- 4. Does the adapter need to add functionality?
example: SWT and Swing.
- 5. Stateful? Probably shouldn't be.