Welcome to Design Patterns 2011.11.18 Design Patterns Ngo Doan Lap
Welcome to Design PatternsWelcome to Design Patterns
2011.11.18
Design Patterns
Ngo Doan Lap
ContentsContentsI. Introduce to design pattern through example
1. Problem
2. Issues
3. Solutions
II. Design patterns
III. Design patterns classification
ProblemProblem1. Simulate Duck application
SimUDuck
1. show a large variety of duck species swimming
2. making quacking sounds
Initial designSupper class
(abstract class)
Inherited classes
(concrete classes)
1
2
3 4
ProblemProblem
1
2
1. Upgrade the system
Duck can fly
IssuesIssues
1
23
Solution is …inheritance?Solution is …inheritance?Override fly() method to do nothing
1
2
No fly, no quack
Disadvantage of inheritance?
3
1. Code is duplicated across subclasses
2. Changes can unintentionally affect others
3. Hard to gain knowledge of all object behaviors
4. Runtime behavior change are difficult
Difficult to maintain
Difficult to maintain
Solution is …interface?Solution is …interface?1. Flyable() interface.
2. Spec will keep changing
3. Override fly() and quack()
What happen if you need to make a little change to the flying
Behavior….in all of 100 the flying Duck subclasses?
1
2
31. Interface solves part of the problem
2. Completely destroys code reuse for those behaviors
Solution is …CHANGESolution is …CHANGENo matter how well you design an application, over time an
application must grow and change or it will die.
StableChange
1
2
3
4
5
SolutionSolution1 fly() and quack() are the parts of the
Duck class that vary across ducks
Separate change and stable
2
How to design Duck behaviors?
3
What we want:1. Instantiate a particular object and initialize it with specific type of flying behavior2. Can change flying behavior at runtime.
1
2
~ API
use an interface to represent each behavior
3
4
Any behaviors changed will not affected to Duck class and it’s child classes
5
SolutionSolution
Program to an interface vs Program to an implementationProgram to an interface vs Program to an implementation
Program to an implementation
Program to an interface
assign the concrete implementation object at runtime
Have demo as quick as possibleDon’t care about extend/upgrade/maintain
Take time to design but it is flexible
Implementing the Duck BehaviorsImplementing the Duck Behaviorstwo interfaces: FlyBehavior and QuackBehavior
1 2
Implementing the Duck BehaviorsImplementing the Duck Behaviors
1
2
Easy to reuse for other objects (not duck)
Easy to extend/modify that make no side effect
Integrating the Duck BehaviorIntegrating the Duck BehaviorAdd two instance variables to the Duck class1
2 Implement performQuack()
don’t care what kind of object it is.Care abouthow to quack()!
flyBehavior and quackBehavior instance variables are set
Integrating the Duck BehaviorIntegrating the Duck Behavior3
3.1
3.2
3.3
TestingTesting
TestingTesting
TestingTesting
TestingTesting
Setting behavior dynamicallySetting behavior dynamically
TestingTesting
The Big PictureThe Big Picture
1
2
3
4
Strategy for upgrading
has a
has a
is a
(*)Change behavior at runtime
5
“better than”(*)
(*)
Strategy PatternStrategy Pattern
Example 2Example 2
Example 3Example 3
Design patternsDesign patterns
1. General reusable solution to a commonly occurring problem
within a given context in software design
2. Type of patterns
Creational patterns
Structural patterns
Behavioral patterns
Design patterns classificationDesign patterns classification
Creational design patterns
This design patterns is all about class instantiation
Design patterns classificationDesign patterns classification
Structural design patterns
This design patterns is all about Class and Object composition
Design patterns classificationDesign patterns classificationBehavioral
design patterns:
Class's objects
communication
How to use design patternsHow to use design patterns
Load to your brain
1
2
Recognize places to apply
QAQA
Investigate more about design patterns?
Thank you for your attention!