设计模式复习-工厂方法模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式复习-工厂方法模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?設計模式復習-工廠方法模式
? ? ? ? 相對于簡單工廠,工廠方法是把算法類的實例化延遲到了調用者那去做,調用者根據自己的需要,自己實例化相關的工廠并且生產相關算法。這么做是因為簡單工廠是不滿足OCP的,因為如果想添加新的算法,就得去改工廠函數里面的那個switch,這樣容易在增加新的功能時引入新問題,而工廠方法不會導致這個問題,在算法擴充的時候滿足OCP,不需要改之前原有的東西,直接往上加就行了。
//FactoryMode.h#pragma?once//抽象算法接口class?CinterfaceAlgorithm?{public:virtual?bool?GetValue(const?int?&nNumberA,?const?int?&nNumberB,?int?&nNumberAns)?=?0;};class?CAddition?:?public?CinterfaceAlgorithm?{public:bool?GetValue(const?int?&nNumberA,?const?int?&nNumberB,?int?&nNumberAns)?{nNumberAns?=?nNumberA?+?nNumberB;return?true;}};class?CSubtraction?:?public?CinterfaceAlgorithm?{public:bool?GetValue(const?int?&nNumberA,?const?int?&nNumberB,?int?&nNumberAns)?{nNumberAns?=?nNumberA?-?nNumberB;return?true;}};class?CMultiplication?:?public?CinterfaceAlgorithm?{public:bool?GetValue(const?int?&nNumberA,?const?int?&nNumberB,?int?&nNumberAns)?{nNumberAns?=?nNumberA?*?nNumberB;return?true;}};class?CDivision?:?public?CinterfaceAlgorithm?{public:bool?GetValue(const?int?&nNumberA,?const?int?&nNumberB,?int?&nNumberAns)?{if?(nNumberB?==?0)?{return?false;}nNumberAns?=?nNumberA?/?nNumberB;return?true;}};//工廠接口class?CinterfaceFactory?{public:virtual?CinterfaceAlgorithm?*?GetFactoryBody()?=?0;};class?CAdditionFactory?:?public?CinterfaceFactory?{CinterfaceAlgorithm?*?GetFactoryBody(){return?new?CAddition();}};class?CSubtractionFactory?:?public?CinterfaceFactory?{CinterfaceAlgorithm?*?GetFactoryBody()?{return?new?CSubtraction();}};class?CMultiplicationFactory?:?public?CinterfaceFactory?{CinterfaceAlgorithm?*?GetFactoryBody()?{return?new?CMultiplication();}};class?CDivisionFactory?:?public?CinterfaceFactory?{CinterfaceAlgorithm?*?GetFactoryBody()?{return?new?CDivision();}};#include?"stdafx.h"#include?"FactoryMode.h"#include?<iostream>using?namespace?std;int?main()?{CinterfaceAlgorithm?*pHashMemoryAlgorithm[10]?=?{?0?};CinterfaceFactory?*?pHashMemoryFactory[10]?=?{?0?};int?nAlgorithmId?=?-1,?nFactoryId?=?-1,?nAns?=?0;pHashMemoryFactory[++nFactoryId]?=?new?CAdditionFactory();pHashMemoryAlgorithm[++nAlgorithmId]?=?pHashMemoryFactory[nFactoryId]->GetFactoryBody();pHashMemoryFactory[++nFactoryId]?=?new?CSubtractionFactory();pHashMemoryAlgorithm[++nAlgorithmId]?=?pHashMemoryFactory[nFactoryId]->GetFactoryBody();pHashMemoryFactory[++nFactoryId]?=?new?CMultiplicationFactory();pHashMemoryAlgorithm[++nAlgorithmId]?=?pHashMemoryFactory[nFactoryId]->GetFactoryBody();pHashMemoryFactory[++nFactoryId]?=?new?CDivisionFactory();pHashMemoryAlgorithm[++nAlgorithmId]?=?pHashMemoryFactory[nFactoryId]->GetFactoryBody();for?(int?nPoit?=?0;?nPoit?<=?nAlgorithmId;?nPoit++)?{if?(pHashMemoryAlgorithm[nPoit]?!=?NULL?&&?pHashMemoryAlgorithm[nPoit]->GetValue(1,?5,?nAns))?{cout?<<?nAns?<<?endl;}else?{cout?<<?"Error"?<<?endl;}}for?(int?nPoit?=?0;?nPoit?<=?nAlgorithmId;?nPoit++)?{delete?pHashMemoryAlgorithm[nPoit];delete?pHashMemoryFactory[nPoit];}//getchar();return?0;}?
總結
以上是生活随笔為你收集整理的设计模式复习-工厂方法模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神经网络与机器学习 笔记—Rosenbl
- 下一篇: 设计模式复习-抽象工厂模式