// Purpose. No reuse #include using std::cout; class One { void a() { cout << "a "; } void b() { cout << "b "; } void c() { cout << "c "; } void d() { cout << "d "; } void e() { cout << "e "; } public: void execute() { a(); b(); c(); d(); e(); } }; class Two { void a() { cout << "a "; } void _2() { cout << "2 "; } void c() { cout << "c "; } void _4() { cout << "4 "; } void e() { cout << "e "; } public: void execute() { a(); _2(); c(); _4(); e(); } }; void main( void ) { One first; first.execute(); cout << '\n'; Two second; second.execute(); cout << '\n'; } // a b c d e // a 2 c 4 e #if 0 // Purpose. Template Method design pattern #include using namespace std; class Base { void a() { cout << "a "; } void c() { cout << "c "; } void e() { cout << "e "; } // 2. Steps requiring peculiar implementations are "placeholders" in base class virtual void ph1() = 0; virtual void ph2() = 0; public: // 1. Standardize the skeleton of an algorithm in a base class "template method" void execute() { a(); ph1(); c(); ph2(); e(); } }; class One : public Base { // 3. Derived classes implement placeholder methods /*virtual*/ void ph1() { cout << "b "; } /*virtual*/ void ph2() { cout << "d "; } }; class Two : public Base { /*virtual*/ void ph1() { cout << "2 "; } /*virtual*/ void ph2() { cout << "4 "; } }; void main( void ) { Base* array[] = { &One(), &Two() }; for (int i=0; i < 2; i++) { array[i]->execute(); cout << '\n'; } } // a b c d e // a 2 c 4 e #endif