diff --git a/Template Method/Makefile b/Template Method/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..9161336b91afc69d5b09e6da64e912495ce8b65a --- /dev/null +++ b/Template Method/Makefile @@ -0,0 +1,6 @@ +all: + g++ -g -Wall -o template1 template1_lib.cpp template1_app.cpp + g++ -g -Wall -o template2 template2_lib.cpp template2_app.cpp + +clean: + rm -rf template1 template2 \ No newline at end of file diff --git a/Template Method/template1_app.cpp b/Template Method/template1_app.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ba652577fe79a6d4509614bb55d3fddedc68244 --- /dev/null +++ b/Template Method/template1_app.cpp @@ -0,0 +1,37 @@ +#include "template1_lib.cpp" + +//应用程序开发人员 +class Application +{ + public: + bool Step2() + { + cout << "myStep2" << endl; + return true; + } + + void Step4() + { + cout << "myStep4" << endl; + } +}; + +int main() +{ + Library lib; + Application app; + + lib.Step1(); + + if (app.Step2()) + { + lib.Step3(); + } + + for (int i = 0; i < 4; i++) + { + app.Step4(); + } + + lib.Step5(); +} diff --git a/Template Method/template1_lib.cpp b/Template Method/template1_lib.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cc9e6428e2a1d408c84e4242a33295bfb4f8773 --- /dev/null +++ b/Template Method/template1_lib.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +//程序库开发人员 +class Library +{ + +public: + void Step1() + { + cout << "Step1" << endl; + } + + void Step3() + { + cout << "Step3" << endl; + } + + void Step5() + { + cout << "Step5" << endl; + } +}; \ No newline at end of file diff --git a/Template Method/template2_app.cpp b/Template Method/template2_app.cpp new file mode 100644 index 0000000000000000000000000000000000000000..833618b3cbf61579202eef62648ca5dbc31841b7 --- /dev/null +++ b/Template Method/template2_app.cpp @@ -0,0 +1,30 @@ +#include "template2_lib.cpp" +#include + +using namespace std; + +//应用程序开发人员 +class Application : public Library +{ + protected: + virtual bool Step2() + { + //... 子类重写实现 + cout << "override Step2" << endl; + return true; + } + + virtual void Step4() + { + //... 子类重写实现 + cout << "override Step4" << endl; + } +}; + +int main() +{ + Library *pLib = new Application(); + pLib->Run(); + + delete pLib; +} diff --git a/Template Method/template2_lib.cpp b/Template Method/template2_lib.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefbbb258305a4bfcb4b50877a8490d21db94e6e --- /dev/null +++ b/Template Method/template2_lib.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +//程序库开发人员 +class Library +{ + public: + //稳定 template method + void Run() + { + Step1(); + + if (Step2()) + { //支持变化 ==> 虚函数的多态调用 + Step3(); + } + + for (int i = 0; i < 4; i++) + { + Step4(); //支持变化 ==> 虚函数的多态调用 + } + + Step5(); + } + virtual ~Library() {} + + protected: + void Step1() + { //稳定 + cout << "Step1" << endl; + } + void Step3() + { //稳定 + cout << "Step3" << endl; + } + void Step5() + { //稳定 + cout << "Step5" << endl; + } + + virtual bool Step2() = 0; //变化 + virtual void Step4() = 0; //变化 +}; \ No newline at end of file