CS 635: Advanced Object-Oriented Design & Programming |
---|
Template Method | slide # 1 |
Template Method lets subclasses redefine certain steps of an
algorithm without changing the algorithm's structure
class Account { public: void virtual Transaction(float amount) { balance += amount;} Account(char* customerName, float InitialDeposit = 0); protected: char* name; float balance; } class JuniorAccount : public Account { public: void Transaction(float amount) {// put code here} } class SavingsAccount : public Account { public: void Transaction(float amount) {// put code here} } Account* createNewAccount() { // code to query customer and determine what type of // account to create }; main() { Account* customer; customer = createNewAccount(); customer->Transaction(amount);
class Account { public: void virtual Transaction() = 0; } class JuniorAccount : public Account { public void Transaction() { put code here} }
class Account { public: void Transaction(float amount); void virtual TransactionSubpartA(); void virtual TransactionSubpartB(); void virtual TransactionSubpartC(); } void Account::Transaction(float amount) { TransactionSubpartA(); TransactionSubpartB(); TransactionSubpartC(); // EvenMoreCode; } class JuniorAccount : public Account { public: void virtual TransactionSubpartA(); } class SavingsAccount : public Account { public: void virtual TransactionSubpartC(); } Account* customer; customer = createNewAccount(); customer->Transaction(amount);
Account* customer; customer = createNewAccount(); customer->Transaction(amount);
Account* customer;
customer = createNewAccount();
customer->Foo(); // compile error