Strategy meets Template
(New page: The UML example demonstrating solutions to transportation: image:Strategy-Template.PNG In this diagram, TransprtSolutionDemo is the main class with a main() method. This class is ...) |
|||
Line 18: | Line 18: | ||
(the method is implemented in both implementation classes). These solve() methods in | (the method is implemented in both implementation classes). These solve() methods in | ||
TransportMethod1 and TransportMethod2 are implemented using the Template Method pattern. | TransportMethod1 and TransportMethod2 are implemented using the Template Method pattern. | ||
− |
Revision as of 09:47, 18 October 2010
The UML example demonstrating solutions to transportation:
In this diagram, TransprtSolutionDemo is the main class with a main() method. This class is coupled
to the TransprtSolution interface. This interface is implemented by two abstract classes:
TransportMethod1 and TransportMethod2. Both abstract classes are extended by two subclasses:
TransprtImpl1 and TransprtImpl2, respectively. By comparing this diagram with the structural diagram
of the Strategy pattern, it is clear that TransprtSolution can be identified as Strategy, TransportMethod1
and TransportMethod2 can be identified as ConcreteStrategy (together with their subclasses), and
TransprtSolutionDemo can be identified as Context (client). The whole problem demonstrates solutions
to transportation. But there are two solutions: these are implemented in TransportMethod1 (with
TransprtImpl1) and in TransportMethod2 (with TransprtImpl2). TransprtSolutionDemo holds a
reference to TransprtSolution and creates objects of both implementations. With a simple call of the
method solve() of the TransprtSolution interface, the solution will be calculated by both algorithms
(the method is implemented in both implementation classes). These solve() methods in
TransportMethod1 and TransportMethod2 are implemented using the Template Method pattern.