design patterns
play

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


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

  2. modes of normal composition fitness for future

  3. Creational Structural Behavioural Abstract Factory Adapter Chain of Responsibility Builder Bridge Command Factory Method Composite Interpreter Prototype Decorator Iterator Singleton Facade Mediator Flyweight Memento Proxy Observer State Strategy Template Method Visitor

  4. Creational Structural Behavioural Adapter Bridge Command Composite Interpreter Singleton Facade Observer Strategy Visitor

  5. Creational Structural Behavioural Adapter Bridge Command Singleton Facade Observer Strategy

  6. Creational Structural Behavioural Adapter Bridge Singleton Observer Strategy

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

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

  9. Observer (Publish/Subscribe) Design challenge: the observers need to know more

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

  11. Observer (Publish/Subscribe) Complications: Need to add new parameters in all existing Listeners May be sending unused data to Listeners

  12. Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 2: callbacks

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

  14. Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 3: reify the event

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

  16. Strategy Vary the algorithm independently of the clients who use it.

  17. Strategy Vary the algorithm independently of the clients who use it.

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

  19. Strategy Who chooses the strategy? a. client b. context c. config file (not pictured)

  20. Strategy Mutable Strategy Objects Stateless Strategy Objects easier to return more reusable complex results re-entrant need to be instantiated for simpler API usage rules each use can be Singleton

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

  22. Singleton Advantages Issues convenience global variables controlled access make testing harder reduced namespace synchronization can substitute alternatives may reduce parallelism more flexible than static memory leaks methods initialization class-loaders vs VMs distributed systems may hinder re-use

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

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

  25. Adapter (Wrapper) Convert the interface of a class into another interface clients expect.

  26. Adapter

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

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

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

  30. Adapter vs Bridge Adapter Bridge connection unforseen connection forseen interfaces already exist need to be able to subclass both abstraction and implementation

  31. Other patterns you need to know Lectures so far & Lab1 Reading & Future Lecture visitor facade [cf Bowman] interpreter command iterator composite

  32. 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? Answer: Observer Design Pattern is used. All the Observers (or Listeners in Java) are notified.

  33. 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? Answer: Strategy Design Pattern

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