Template_Method.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: TEMPLATE METHOD
  4. * http://www.plantuml.com/plantuml/uml/PL5BJiCm4Dtx52EnAAXjedPPHL4_hSI2KXQvWECCn99Z1tjIgGBkGy-HYp59gGJDhCRFxwEdMGQIdfhAY7W4sMx_yhJATh3VPO-7BOnYmQEC305MUI0l5MtwLq9OrKOIxf5ALzm_W9B6mA56BqaxEvlC9r4KfV0XW6SCj_mUlliZ649pXhwkyHjdbKV2I_pLTI0V3-IbBX0ud4vGOPMZ3qBSTFcSJl_x2s4T8SW0ppC1F4cU9iePvnVUdQHPcmRJxBSHN2ebSKz8apXFhvhiLEEY0s2DfJngvwNf_xRj-LLRJT1OaERDUKrbXH3U6pH8x5auIptJO82gFNS9l1sr9wPqO606QCldiqgtPqPgwS58w6Mggu6WkbRjDs4aq1RGjH9BtlZUFm00
  5. *
  6. */
  7. #include <iostream>
  8. class AbstractClass {
  9. public:
  10. virtual ~AbstractClass() {}
  11. void templateMethod() {
  12. std::cout << "generic design step1" << std::endl;
  13. Operation1();
  14. std::cout << "generic design step2" << std::endl;
  15. Operation2();
  16. }
  17. virtual void Operation1() = 0;
  18. virtual void Operation2() = 0;
  19. };
  20. class ConcreteClass : public AbstractClass {
  21. public:
  22. ~ConcreteClass() {}
  23. void Operation1() {
  24. std::cout << "ConcreteClass Operation 1" << std::endl;
  25. }
  26. void Operation2() {
  27. std::cout << "ConcreteClass Operation 2" << std::endl;
  28. }
  29. };
  30. int main() {
  31. AbstractClass *templateClass = new ConcreteClass();
  32. templateClass->templateMethod();
  33. }