240 likes | 329 Views
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?.
E N D
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?
The Singleton Pattern • Ensures a class has only one instance, and provide a global point of access to it.
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
The Problem Problem: Sort a list of People How many different ways can you sort a list of people?
What’s Wrong Hint – Think Open Closed Principle
The Strategy Pattern Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy Tradeoffs Benefits Remove the need to subclass Eliminate conditional processing. Liabilities Users of List must be aware of different strategies Potential class explosion.
The Problem • An object in a program has an internal "state," and the behavior of the object needs to change when its state changes.
The State Pattern • Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class.
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)
The Problem • How do I convert the interface of one class into another interface that the client can work with?
The Adapter Pattern • Converts the interface of a class into another interface the client expects.
The Adapter Pattern * Diagram shamelessly lifted from Head First Design Patterns
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.
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
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!!)