diff --git a/23.TemplateMethodPattern/1.Picture/模板方法模式UML图.png b/23.TemplateMethodPattern/1.Picture/模板方法模式UML图.png new file mode 100644 index 0000000..55eea9b Binary files /dev/null and b/23.TemplateMethodPattern/1.Picture/模板方法模式UML图.png differ diff --git a/23.TemplateMethodPattern/1.Picture/模板方法模式实例UML图.png b/23.TemplateMethodPattern/1.Picture/模板方法模式实例UML图.png new file mode 100644 index 0000000..1fe8070 Binary files /dev/null and b/23.TemplateMethodPattern/1.Picture/模板方法模式实例UML图.png differ diff --git a/23.TemplateMethodPattern/2.Code/Demo.h b/23.TemplateMethodPattern/2.Code/Demo.h new file mode 100644 index 0000000..83fe157 --- /dev/null +++ b/23.TemplateMethodPattern/2.Code/Demo.h @@ -0,0 +1,41 @@ +#ifndef __DEMO_H__ +#define __DEMO_H__ + +// ࣩࣨ +class AbstractClass +{ +public: + // ģ巽һ㷨Ŀ + void templateMethod(){ + // do something + method1(); + method2(); + method3(); + } + // + void mehtod1(){ + // do something + } + // 2 + virtual void method2() = 0; + // 3Ĭʵ + void mehtod3(){ + // do something + } +}; + +// ࣨ) +class ConcreteClass :public AbstractClass +{ +public: + // ʵֻ2 + void method2(){ + // do something + } + // ض3ǻķ3 + void method3(){ + // do something + } +}; + +#endif \ No newline at end of file diff --git a/23.TemplateMethodPattern/2.Code/FingerprintModule.h b/23.TemplateMethodPattern/2.Code/FingerprintModule.h new file mode 100644 index 0000000..1858827 --- /dev/null +++ b/23.TemplateMethodPattern/2.Code/FingerprintModule.h @@ -0,0 +1,97 @@ +#ifndef __FINGERPRINTMODULE_H__ +#define __FINGERPRINTMODULE_H__ + +#include + +// +class FingerprintModule +{ +public: + FingerprintModule(){} + void getImage(){ + printf("ָͼ\n"); + } + void output(){ + printf("ָͼ!\n\n"); + } + virtual bool isSafeMode() = 0; + virtual void processImage() = 0; + // ӽ + virtual void encrypt() = 0; + virtual void decrypt() = 0; + + // ģ巽 + void algorithm(){ + // 1.ͼ + getImage(); + // 2.ȫģʽ¼ܺͽ + if (isSafeMode()){ + // 2.1. + encrypt(); + // 2.2. + decrypt(); + } + // 3.Image + processImage(); + // 4. + output(); + } +}; + +// +class FingerprintModuleA :public FingerprintModule +{ +public: + FingerprintModuleA(){} + void processImage(){ + printf("ʹ һ汾㷨 ָͼ\n"); + } + bool isSafeMode(){ + printf("ȫģʽ\n"); + return true; + } + void encrypt(){ + printf("ʹRSAԿ\n"); + } + void decrypt(){ + printf("ʹRSAԿ\n"); + } +}; + +// +class FingerprintModuleB :public FingerprintModule +{ +public: + FingerprintModuleB(){} + void processImage(){ + printf("ʹ ڶ汾㷨 ָͼ\n"); + } + bool isSafeMode(){ + printf("ǰȫģʽ\n"); + return false; + } + void encrypt(){} + void decrypt(){} +}; + +// +class FingerprintModuleC :public FingerprintModule +{ +public: + FingerprintModuleC(){} + void processImage(){ + printf("ʹ һ汾㷨 ָͼ\n"); + } + bool isSafeMode(){ + printf("ȫģʽ\n"); + return true; + } + void encrypt(){ + printf("ʹDHԿ\n"); + } + void decrypt(){ + printf("ʹDHԿ\n"); + } +}; + +#endif //__FINGERPRINTMODULE_H__ \ No newline at end of file diff --git a/23.TemplateMethodPattern/2.Code/main.cpp b/23.TemplateMethodPattern/2.Code/main.cpp new file mode 100644 index 0000000..ffca5f3 --- /dev/null +++ b/23.TemplateMethodPattern/2.Code/main.cpp @@ -0,0 +1,19 @@ +#include "FingerprintModule.h" +#include + +int main() +{ + FingerprintModule *fp = new FingerprintModuleA(); + fp->algorithm(); + + fp = new FingerprintModuleB(); + fp->algorithm(); + + fp = new FingerprintModuleC(); + fp->algorithm(); + + + printf("\n\n"); + system("pause"); + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 1ab3adb..a213084 100644 --- a/README.md +++ b/README.md @@ -101,4 +101,8 @@ Jungle设计模式系列 25.设计模式(二十五)——策略模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102984862 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102984862 + +26.设计模式(二十六)——模板方法模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102994585 \ No newline at end of file