An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant http://www.jpboodhoo.com/blog [email protected] The Problem How do I ensure that a class has only one instance of itself.
Download ReportTranscript An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant http://www.jpboodhoo.com/blog [email protected] The Problem How do I ensure that a class has only one instance of itself.
An Introduction To Design Patterns Jean-Paul S. Boodhoo Independent Consultant http://www.jpboodhoo.com/blog [email protected] 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. Hello Singleton 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? Naive Sorting What’s Wrong Hint – Think Open Closed Principle The Strategy Pattern Define a family of algorithms, encapsulate each one, and make them interchangeable. Context ConcreteStrategyA Strategy ConcreteStrategyB ConcreteStrategyC Sorting With The Strategy Pattern 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. Messy State Implementation The State Pattern Allows an object to alter its behaviour when its internal state changes. The object will appear to change its class. Applying The State Pattern 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 Applying The Adapter Pattern 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 Design Patterns Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Addison-Wesley, 1995 Enterprise Integration Patterns Gregor Hohpe, Bobby Woolf Addison-Wesley, 2004 Patterns of Enterprise Application Architecture Martin Fowler Addison-Wesley, 2003 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!!)