Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between the patterns Strategy, Visitor and Template Method?

I'm in a class where we just learned about these design patterns. However I couldn't see any difference between them. They sound just like the same, creating concrete classes over the abstract one. Could somebody help me kill this doubt? thanks (:

like image 482
Hugo Mota Avatar asked Nov 24 '10 12:11

Hugo Mota


People also ask

What is the point of difference between the strategy and template method design patterns?

But, the key difference is that Strategy Pattern is about modifying a behaviour of a context in runtime using strategies, while Template Method Pattern is about following a skeleton implementation of an algorithm and modifying its behaviour by overriding methods of the skeleton class in the subclasses.

What is the main difference between how the template method pattern and the strategy pattern are typically implemented in code?

The difference between the two is that while the strategy pattern allows different implementations to use completely different ways of the achieving the desired outcome, the template method pattern specifies an overarching algorithm (the "template" method) which is be used to achieve the result -- the only choice left ...

What is the difference between a pattern and a template?

A template is a master pattern (in word processing a standard sequence of words, phrases or sentences) which is copied by the user, who can normally add something (e.g. someone's name) to it. A pattern (as used in social security numbers) is a sequence of data that follows a rule.


2 Answers

Both the visitor, the strategy, and the template pattern encompass the application of an algorithm. The biggest difference is in how they are evoked and how they are used in practice. While it may seem like they have the same use case, look at the construction of the objects to see the difference.

The strategy pattern is often used when we don't have the ability to pass around functions as a first class object. It expects a very specific argument list and only that argument list in its call pattern. For instance:

struct MyStrat{
    void operator()(const Foo &_input){
        _input.up( 2 );
    }
};

std::for_each( myFooList.begin(), myFooList.end(), MyStrat() );

which is then applied to a list of objects of type "Foo." We really have no other way of applying it to any other object.

The visitor pattern on the other hand is used when we want to apply an algorithm to a bunch of objects that might not share the same signature nor have the same member functions. We say visitor pattern because it's often used when traversing a tree or another collection of "unrelated" objects (unrelated in an inheritance sense.)

struct MyVisitor{
    void visit(const Foo &_input){
         _input.up( 2 );
    }
    void visit(const Bar &_input){
         _input.raiseUp( 2 );
    }
    void visit(const Baz &_input){
         _input.setUp( 2 );
    }
 };

Here, the idea is that we'd like to "up" all these objects. They all don't share the same member function signature but all are conceptually related. Hence, we can "visit" each of these classes but expect the algorithm to perform the same type of task.

By using a visitor pattern we avoid the need to wrap each class in a proxy pattern. Hence, for N classes we'd like to apply this algorithm to we don't need to make N proxy classes. We only need to add N methods to a visitor class.

The template method is quite different from either the visitor and the strategy pattern. With the template what you're trying to do is enforce the same type of algorithm but on different subclasses within a hierarchy. For instance:

class Duck{
public:
    int count() =0;
    void makeNoise(int times) =0;
    void quack(){ makeNoise( count() ); }//the template pattern is here
};

class Mallard : public Duck{
public:
    int count(){ return 4; }
    void makeNoise( cout << "quack" << endl; }
};

class Daffy{
public:
    int count(){ return 1; }
    void makeNoise( cout << "Why I ought to..." << endl; }
};

So the result of the algorithm varies within the heirarchy.

like image 103
wheaties Avatar answered Sep 28 '22 01:09

wheaties


Commonalities:

  1. Strategy, Template method and Visitor : All three patterns are categorized as behavioural patterns.

Differences:

  1. Template method uses Inheritance and Strategy uses composition
  2. The Template method implemented by the base class should not be overridden. In this way, the structure of the algorithm is controlled by the super class, and the details are implemented in the sub classes
  3. Strategy encapsulates the algorithm behind an interface, which provide us ability to change the algorithm at run time
  4. Visitor pattern is used to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class
  5. If there is a change in implementation of Operation, we have to just change Visitor class instead of touching all other objects.

Have a look at Template method , Strategy and Visitor and Sourcemaking articles for better understanding.

Related posts:

When should I use the Visitor Design Pattern?

Real World Example of the Strategy Pattern

Template design pattern in JDK, could not find a method defining set of methods to be executed in order

like image 45
Ravindra babu Avatar answered Sep 28 '22 00:09

Ravindra babu