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.

  • 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). – Filip Milovanović Dec 5 at 19:02

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 ;-)

  • 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 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 at 20:41

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.

  • "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 at 20:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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