An Introduction To Design Patterns

24
An Introduction To Design Patterns Jean-Paul S. Boodhoo Jean-Paul S. Boodhoo Independent Consultant Independent Consultant http://www.jpboodhoo.com/blog [email protected] [email protected]

description

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?. - PowerPoint PPT Presentation

Transcript of An Introduction To Design Patterns

Page 1: An Introduction To Design Patterns

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

Page 2: An Introduction To Design Patterns
Page 3: An Introduction To Design Patterns

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?

Page 4: An Introduction To Design Patterns

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.

Page 5: An Introduction To Design Patterns

Hello SingletonHello SingletonHello SingletonHello Singleton

Page 6: An Introduction To Design Patterns

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

Page 7: An Introduction To Design Patterns

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?

Page 8: An Introduction To Design Patterns

Naive SortingNaive SortingNaive SortingNaive Sorting

Page 9: An Introduction To Design Patterns

What’s WrongWhat’s Wrong

Hint – Think Open Closed PrincipleHint – Think Open Closed Principle

Page 10: An Introduction To Design Patterns

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

Page 11: An Introduction To Design Patterns

Sorting With The Strategy Sorting With The Strategy PatternPatternSorting With The Strategy Sorting With The Strategy PatternPattern

Page 12: An Introduction To Design Patterns

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.

Page 13: An Introduction To Design Patterns

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.

Page 14: An Introduction To Design Patterns

Messy State Messy State ImplementationImplementationMessy State Messy State ImplementationImplementation

Page 15: An Introduction To Design Patterns

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.

Page 16: An Introduction To Design Patterns

Applying The State PatternApplying The State PatternApplying The State PatternApplying The State Pattern

Page 17: An Introduction To Design Patterns

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)

Page 18: An Introduction To Design Patterns

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?

Page 19: An Introduction To Design Patterns

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.

Page 20: An Introduction To Design Patterns

The Adapter PatternThe Adapter Pattern

* * Diagram shamelessly lifted from Head First Design PatternsDiagram shamelessly lifted from Head First Design Patterns

Page 21: An Introduction To Design Patterns

Applying The Adapter Applying The Adapter PatternPatternApplying The Adapter Applying The Adapter PatternPattern

Page 22: An Introduction To Design Patterns

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.

Page 23: An Introduction To Design Patterns

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

Page 24: An Introduction To Design Patterns

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!!)