Skip to content

Instantly share code, notes, and snippets.

@alexprivalov
Created January 25, 2015 22:21
Show Gist options
  • Save alexprivalov/01c7522e5f75ec3a8239 to your computer and use it in GitHub Desktop.
Save alexprivalov/01c7522e5f75ec3a8239 to your computer and use it in GitHub Desktop.
#include <iostream>
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:
virtual ~Base() {}
// 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 ";
}
};
int main()
{
Base *array[] =
{
new One(), new Two()
};
for (int i = 0; i < 2; i++)
{
array[i]->execute();
cout << '\n';
}
for (int i = 0; i < 2; i++)
{
delete array[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment