4

Do we have pattern, which is opposite to Template Method?

I mean, in base class we define parts of algorithm and abstract method which implements algorithm. Then in derived class, in that abstract method, we can mix those parts as we want.

1
  • Sounds like Strategy, with some shared behavior pushed up the hierarchy to the (abstract, base) Strategy, but be careful, as all ConcreteStrategy classes will inherit these behaviors weather they need them or not, which can lead to unwanted coupling. Generally speaking, does not seem like a good idea. And inheritance is not necessarily the right tool to use here (depends on what you are trying to do). Dec 5, 2018 at 19:02

3 Answers 3

2

What you describe is basic polymorphism:

class Receipee {
public: 
    void get_eggs(int n) { cout << "I take "<<n<<" eggs" <<endl; }
    void get_chocolate (int weight) { cout << "I take "<<weight<<" g chocolate" <<endl; }
    void get_milk(int l) { cout << "I take "<<l<<" cl of milk" <<endl; }
    void mix_it(int force) { cout << "I mix everything force "<<force<<endl; }
    virtual void prepare() = 0; 
};

class Receipee1 : public Receipee {
public: 
    void prepare() override { 
         get_milk(100);  
         get_eggs(2); 
         cout << "I do some mystery trick"<<endl; 
         mix_it(3); 
         cout << "Et voilà !" <<endl; 
    } 
};

No need for pattern for that ;-)

2
  • 1
    It may be debatable if get_eggs etc needs to be public. protected might be enough (except, one would like to unit-test those methods directly).
    – Doc Brown
    Dec 5, 2018 at 19:34
  • @DocBrown you are of course right. It was purely illustrative. But I'm sure there are some examples where public could make more sense ;-)
    – Christophe
    Dec 5, 2018 at 20:41
1

It's the strategy pattern.  In the strategy pattern, the main method of the base (which implements the algorithm) is abstract — the algorithm is meant to be implemented by subs. Whereas in template method pattern, this main method is concrete/dictated while only piece parts are abstract. 

These patterns are both thoughtful application of class hierarchy and overriding.  However, to be sure, many variations are possible to suit various needs.  So, we can have abstract and concrete methods defined in the base to be used as needed (as helpers and/or as mains).  When the main a algorithm is concrete in the base we call it template, and when abstract we call it strategy.  Sometimes we mix both, meaning a concrete yet override-able main in the base.

1
  • "When the main a algorithm is concrete in the base we call it template, and when abstract we call it strategy. " But do Strategy pattern has already implemented pieces of algorithm? If yes, do we define them in contract?
    – Yurii N.
    Dec 6, 2018 at 20:12
0

From my understanding, the Template Method is a KIND of Strategy Pattern. The difference is they both provide solutions to a "plug and play" algorithm problem.

Although there is a general rule of thumb in that we should favor Composition over Inheritance. People much smarter than myself have pointed out that, over the long term inheritance can provide more challenges than solutions. So rather than, templating your code, you should opt to inject the algorithm (or strategies) as you need them. In short, you should use the strategy pattern if you're looking to keep things decoupled and flexible and inject the behaviors according to your Use Case.

2
  • You're totally right about "composition over inheritance", but the question was not really about that ;)
    – Yurii N.
    Dec 18, 2018 at 17:45
  • I answered. :) Strategy. Template is a more rigid algorithm with "plugable" methods which you can implement through overrides in your subclass. Strategy is opposite in the sense that it isn't inheritance AND it's not rigid. Since the primitive methods called might be called in the base class, it means that the template essentially has you filling in the primitive methods or overwriting the hooks of the base. Strategy, simply has you injecting the implementation, offering complete flexibility. It's opposite in the sens of intent. One is a jigsaw puzzle you fill, the other is blocks you stack. Dec 18, 2018 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.