1 / 24

An Introduction To Design Patterns

An Introduction To Design Patterns. Jean-Paul S. Boodhoo Independent Consultant http://www.jpboodhoo.com/blog bitwisejp@gmail.com. The Problem. How do I ensure that a class has only one instance of itself active for the application lifetime, as well as providing access to it?.

Download Presentation

An Introduction To Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant http://www.jpboodhoo.com/blog bitwisejp@gmail.com

  2. The Problem • How do I ensure that a class has only one instance of itself active for the application lifetime, as well as providing access to it?

  3. The Singleton Pattern • Ensures a class has only one instance, and provide a global point of access to it.

  4. Hello Singleton

  5. Singleton Tradeoffs Benefits Controlled access to a sole instance Bye bye global variables Liabilities Multihreading Issues Tighter coupling when not done correctly Difficult to test when not implemented correctly

  6. The Problem Problem: Sort a list of People How many different ways can you sort a list of people?

  7. Naive Sorting

  8. What’s Wrong Hint – Think Open Closed Principle

  9. The Strategy Pattern Define a family of algorithms, encapsulate each one, and make them interchangeable.

  10. Sorting With The Strategy Pattern

  11. Strategy Tradeoffs Benefits Remove the need to subclass Eliminate conditional processing. Liabilities Users of List must be aware of different strategies Potential class explosion.

  12. The Problem • An object in a program has an internal "state," and the behavior of the object needs to change when its state changes.

  13. Messy State Implementation

  14. The State Pattern • Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.

  15. Applying The State Pattern

  16. State Tradeoffs • Benefits • New states can be added easily • Goodbye messy conditionals • Liabilities • Potential class explosion • Coupling between state classes (may not be a bad thing)

  17. The Problem • How do I convert the interface of one class into another interface that the client can work with?

  18. The Adapter Pattern • Converts the interface of a class into another interface the client expects.

  19. The Adapter Pattern * Diagram shamelessly lifted from Head First Design Patterns

  20. Applying The Adapter Pattern

  21. Many Ways to Implement You can implement a design pattern 100 times and have it come out different each time. Start simple, don’t make your designs unnecessarily complicated.

  22. Pattern Catalogs Patterns of Enterprise Application ArchitectureMartin FowlerAddison-Wesley, 2003 Design Patterns Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Addison-Wesley, 1995 Enterprise Integration Patterns Gregor Hohpe, Bobby WoolfAddison-Wesley, 2004 Head First Design Patterns Eric Freeman & Elisabeth Freeman O’Reilly, 2004

  23. Resources Design Pattern – Gamma,Helm,Johnson,Vlissides Head First Design Patterns – Eric Freeman and Elisabeth Freeman Refactoring to Patterns – Joshua Kerievsky www.jpboodhoo.com – JP Boodhoo’s Web Site (had to throw that one in!!)

More Related