An Introduction To Design PatternsAn Introduction To Design Patterns
Jean-Paul S. BoodhooJean-Paul S. BoodhooIndependent ConsultantIndependent Consultanthttp://www.jpboodhoo.com/[email protected]@gmail.com
The ProblemThe Problem
How do I ensure that a class has only one How do I ensure that a class has only one instance of itself active for the application instance of itself active for the application lifetime, as well as providing access to it?lifetime, as well as providing access to it?
The Singleton PatternThe Singleton Pattern
Ensures a class has only one instance, and Ensures a class has only one instance, and provide a global point of access to it.provide a global point of access to it.
Hello SingletonHello SingletonHello SingletonHello Singleton
Singleton TradeoffsSingleton Tradeoffs
BenefitsBenefitsControlled access to a sole instanceControlled access to a sole instance
Bye bye global variablesBye bye global variables
LiabilitiesLiabilitiesMultihreading IssuesMultihreading Issues
Tighter coupling when not done correctlyTighter coupling when not done correctly
Difficult to test when not implemented correctlyDifficult to test when not implemented correctly
The ProblemThe Problem
Problem: Sort a list of PeopleProblem: Sort a list of People
How many different ways can you sort a How many different ways can you sort a list of people?list of people?
Naive SortingNaive SortingNaive SortingNaive Sorting
What’s WrongWhat’s Wrong
Hint – Think Open Closed PrincipleHint – Think Open Closed Principle
The Strategy PatternThe Strategy Pattern
Define a family of algorithms, encapsulate Define a family of algorithms, encapsulate each one, and make them each one, and make them interchangeable. interchangeable.
Context Strategy
ConcreteStrategyA ConcreteStrategyB ConcreteStrategyC
Sorting With The Strategy Sorting With The Strategy PatternPatternSorting With The Strategy Sorting With The Strategy PatternPattern
Strategy TradeoffsStrategy Tradeoffs
BenefitsBenefitsRemove the need to subclassRemove the need to subclass
Eliminate conditional processing.Eliminate conditional processing.
LiabilitiesLiabilitiesUsers of List must be aware of different Users of List must be aware of different strategiesstrategies
Potential class explosion.Potential class explosion.
The ProblemThe Problem
An object in a program has an internal An object in a program has an internal "state," and the behavior of the object "state," and the behavior of the object needs to change when its state changes. needs to change when its state changes.
Messy State Messy State ImplementationImplementationMessy State Messy State ImplementationImplementation
The State PatternThe State Pattern
Allows an object to alter its behaviour Allows an object to alter its behaviour when its internal state changes. The object when its internal state changes. The object will appear to change its class.will appear to change its class.
Applying The State PatternApplying The State PatternApplying The State PatternApplying The State Pattern
State TradeoffsState Tradeoffs
BenefitsBenefitsNew states can be added easilyNew states can be added easily
Goodbye messy conditionalsGoodbye messy conditionals
LiabilitiesLiabilitiesPotential class explosionPotential class explosion
Coupling between state classes (may not be a Coupling between state classes (may not be a bad thing)bad thing)
The ProblemThe Problem
How do I convert the interface of one class How do I convert the interface of one class into another interface that the client can into another interface that the client can work with?work with?
The Adapter PatternThe Adapter Pattern
Converts the interface of a class into Converts the interface of a class into another interface the client expects. another interface the client expects.
The Adapter PatternThe Adapter Pattern
* * Diagram shamelessly lifted from Head First Design PatternsDiagram shamelessly lifted from Head First Design Patterns
Applying The Adapter Applying The Adapter PatternPatternApplying The Adapter Applying The Adapter PatternPattern
Many Ways to Implement Many Ways to Implement
You can implement a design You can implement a design pattern 100 times and have it pattern 100 times and have it come out different each time.come out different each time.
Start simple, don’t make your Start simple, don’t make your designs unnecessarily designs unnecessarily complicated.complicated.
Design Patterns Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Addison-Wesley, 1995
Patterns of Enterprise Application Architecture Martin FowlerAddison-Wesley, 2003
Enterprise Integration Patterns Gregor Hohpe, Bobby WoolfAddison-Wesley, 2004
Pattern CatalogsPattern Catalogs
Head First Design PatternsEric Freeman & Elisabeth FreemanO’Reilly, 2004
ResourcesResources
Design Pattern – Design Pattern – Gamma,Helm,Johnson,VlissidesGamma,Helm,Johnson,Vlissides
Head First Design Patterns – Eric Freeman Head First Design Patterns – Eric Freeman and Elisabeth Freemanand Elisabeth Freeman
Refactoring to Patterns – Joshua KerievskyRefactoring to Patterns – Joshua Kerievsky
www.jpboodhoo.com – JP Boodhoo’s Web www.jpboodhoo.com – JP Boodhoo’s Web Site (had to throw that one in!!)Site (had to throw that one in!!)
Top Related