Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created December 30, 2017 14:05
Show Gist options
  • Save FONQRI/aada609246dc80d5158f3fef4b54801d to your computer and use it in GitHub Desktop.
Save FONQRI/aada609246dc80d5158f3fef4b54801d to your computer and use it in GitHub Desktop.
template design pattern c++
#include <iostream>
using namespace std;
// Base class
// Template class
class Account {
public:
// Abstract Methods
virtual ~Account();
virtual void Start() = 0;
virtual void Allow() = 0;
virtual void End() = 0;
virtual int MaxLimit() = 0;
// Template Method
void Withdraw(int amount)
{
Start();
int limit = MaxLimit();
if (amount < limit) {
Allow();
}
else {
cout << "Not allowed" << endl;
}
End();
}
};
Account::~Account() {}
// Derived class
class AccountNormal : public Account {
public:
~AccountNormal();
void Start();
void Allow();
void End();
int MaxLimit();
};
AccountNormal::~AccountNormal() {}
void AccountNormal::Start() { cout << "Start ..." << endl; }
void AccountNormal::Allow() { cout << "Allow ..." << endl; }
void AccountNormal::End() { cout << "End ..." << endl; }
int AccountNormal::MaxLimit() { return 1000; }
// Derived class
class AccountPower : public Account {
public:
~AccountPower();
void Start();
void Allow();
void End();
int MaxLimit();
};
AccountPower::~AccountPower() {}
void AccountPower::Start() { cout << "Start ..." << endl; }
void AccountPower::Allow() { cout << "Allow ..." << endl; }
void AccountPower::End() { cout << "End ..." << endl; }
int AccountPower::MaxLimit() { return 5000; }
int main()
{
AccountPower power;
power.Withdraw(1500);
AccountNormal normal;
normal.Withdraw(1500);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment