Java Reference
In-Depth Information
well-known Strategy pattern (Gamma et al . 1995). In addition, we have seen
how exceptions can help in dealing with abnormal situations and how they
allow a more readable and clean handling of errors.
Idiom
Class generalization
Context
Many classes have common features.
Problem
The replication of the same features makes classes less
understandable and maintainable.
Forces or tradeoffs
The same attribute or method is repeated many times in
different classes.
Some operations are common to many classes.
Solution
Create a superclass of all the existing similar classes.
Migrate the common characteristics into the superclass.
Force resolution
Common features are not replicated. The superclass
becomes the single point of change. Derived classes are
simpler. Code reuse is enforced. Gathering common
features makes the structure more comprehensible.
Design rationale
We find commonalities among existing classes and make
them into a single class.
Pattern
Strategy
Context
A class embeds a behaviour that can be chosen and or
changed at run time.
Problem
Switching the behaviour dynamically.
Lets the behaviour vary independently from clients that
use it.
Forces or tradeoffs
The clients need a stable interface. The behaviour must be
changeable.
The multiple behaviours must be evident from the
structure of the classes.
Solution
Define a common interface for all the different behaviours
( Strategy ). For each behaviour define a class that
implements the interface ( ConcreteStrategyA ,
ConcreteStrategyB , ...). The behaviours are used by a
client that defines the context within which they operate
( Context ). To select a behaviour an object of one of the
behaviour classes can be linked to the Context .
! strategy
<<interface>>
Strategy
Context
! operation()
ConcreteStrategyA
ConcreteStrategyB
! operation()
! operation()
Search WWH ::




Custom Search