design patterns learning goals
play

Design Patterns Learning Goals Understand what are design patterns, - PowerPoint PPT Presentation

CPSC 310 Software Engineering Lecture 11 Design Patterns Learning Goals Understand what are design patterns, their benefits and their drawbacks For at least the following design patterns: Singleton, Observer, Adapter, you will be


  1. CPSC 310 – Software Engineering Lecture 11 Design Patterns

  2. Learning Goals ● Understand what are design patterns, their benefits and their drawbacks ● For at least the following design patterns: Singleton, Observer, Adapter, you will be able to describe them, know when to use them or not and give examples of situations where you could use them. 2/26

  3. A bit of history ” Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” Christopher Alexander “ A pattern expresses a relation between a certain context, a problem and a solution” 3/26

  4. A bit of history, continued OOP World The “Gang of Four” - GoF Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design patterns: elements of reusable object-oriented software 4/26 image designed by Jessica Lock from the Noun Project

  5. T o be, or not to be A Design Pattern IS: – a way to benefit from the collective experience of skilled software developers – an easy way to communicate about common problems A Design Pattern IS NOT: – the complete solution to your problem – the only solution to your problem (but it's a proven one) – something you should use if you do not understand it 5/26

  6. Pattern Classification Creational Patterns How an object can be created Structural Patterns How objects can be composed Behavioral Patterns How objects communicate 6/26

  7. Singleton pattern Intent: Make sure a class has only one instance, and provide a global point of access to it Participants & Structure: Singleton - instance : Singleton - Singleton() + getInstance() : Singleton 7/26

  8. Singleton pattern public class Singleton { private static Singleton instance = null ; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null ) { instance = new Singleton(); } return instance; } } 8/26

  9. Singleton pattern public class Singleton { private static Singleton instance = null ; private Singleton() { } public static synchronized Singleton getInstance() { if (instance == null ) { instance = new Singleton(); } return instance; } } 9/26

  10. Singleton pattern Creational Patterns T o which How an object can be created category Structural Patterns belongs How objects can be composed this Behavioral Patterns pattern ? How objects communicate 10/26

  11. Observer pattern Intent: Ensure that, when an object changes his state, all its dependents are notified and updated automatically Participants & Structure: 11/26

  12. Observer pattern [example] Auctioneer (Subject) 4 8 15 16 Bidders 12/26 (Observers)

  13. Observer pattern [example] Auctioneer (Subject) regist ster bidders 4 8 15 16 Bidders 13/26 (Observers)

  14. Observer pattern [example] Auctioneer (Subject) 1. accept new bid 2. notify new bid 4 8 15 16 Bidders 14/26 (Observers)

  15. Observer pattern [example] Auctioneer (Subject) retrieve new price 8 15 16 4 Bidders 15/26 (Observers)

  16. Observer pattern Creational Patterns T o which How an object can be created category Structural Patterns belongs How objects can be composed this Behavioral Patterns pattern ? How objects communicate 16/26

  17. Adapter pattern Intent: Convert the interface of a class into another interface that clients expect. Participants & Structure: 17/26

  18. Adapter pattern Power supply adapter analogy 18/26

  19. Adapter pattern Concrete example 19/26

  20. Adapter pattern Creational Patterns T o which How an object can be created category Structural Patterns belongs How objects can be composed this Behavioral Patterns pattern ? How objects communicate 20/26

  21. Design Pattern Collection Factory Singleton Decorator Proxy Template Composite Adapter Observer http://sourcemaking.com/design_patterns 21/26

  22. Anti Pattern ● A bad solution to a recurring problem ● "Strong" code smell ● A good pattern in the wrong context can lead to an anti-pattern 22/26

  23. Anti Pattern ● Several categories – Development – Architecture – Management http://c2.com/cgi/wiki?AntiPatternsCatalog http://sourcemaking.com/antipatterns 23/26

  24. Eg: Golden Hammer Problem: You need to choose technologies for your development, and you are of the belief that you must choose ● exactly one technology to dominate the architecture. Context: You need to develop some new system or piece of software that doesn't fit very well the technology that ● the development team are familiar with. Forces: ● The development team are committed to the technology they know – The development team are not familiar with other technologies – Other, unfamiliar, technologies are seen as risky – It is easy to plan and estimate for development in the familiar technology – Suppo posed Solu lutio ion: Use the familiar technology anyway. The technology (or concept) is applied obsessively to ● many problems, including where it is clearly inappropriate. Refactor ored d Solu lution on: Expanding the knowledge of developers through education, training, and book study groups ● exposes developers to new solutions. 24/26 http://sourcemaking.com/antipatterns/golden-hammer

  25. Eg. Singleton Overuse ● Problems – violate information hiding since dependencies are hidden in the code and not expressed in the interface – can cause high coupling ● You must have a good damn reason to use it – The fact that you know it is not enough public void someMethod() ... Profile.getInstance().getUserLevel() public void someMethod(Profile profile) ... 25/26 profile.getUserLevel()

  26. Design Pattern Drawbacks ● Can make the design more complex if not needed – Start simple and then refactor by using a design pattern if it is justified – Do not try to anticipate future needs too much ● Can lead to bad design if not applied in the right context 26/26

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