作者:ACHEn大魔王 | 来源:互联网 | 2023-10-12 15:56
题目描述:Factory动机(motivation)实例化对象的时候不再使用newObject()形式,可以根据用户的选择条件来实例化相关的类。对于客户端来说&
题目描述:
Factory 动机(motivation) 实例化对象的时候不再使用 new Object()形式,可以根据用户的选择条件来实例化相关的类。对于客户端来说,去除了具体的类的依赖。只需要给出具体实例的描述给工厂,工厂就会自动返回具体的实例对象。
模式定义 根据需求创建对象的种类。 ——《设计模式》GoF
要点总结 有时会和其他模式解和使用,我们无需提供具体的子类类名,只需要提供一个字符串即可得到相应的实例对象。这样的话,当子类的类名更换或者增加子类时我们都无需修改客户端代码,只需要在简单工厂类上增加一个分支判断代码即可。
代码结构 . ├── build. sh ├── clearBuild. sh ├── CMakeLists. txt ├── src │ ├── examStu. cpp │ ├── include │ │ └── examStu. h │ └── main. cpp
源码例子 examStu.h
#ifndef _EXANSTU__ #define _EXANSTU__ #include #include #include using namespace std; typedef enum _eShoesType{ SHOES_TYPE_BASE &#61; 0 , SHOES_TYPE_NIKE &#61; 1 , SHOES_TYPE_ADIDAS &#61; 2 , SHOES_TYPE_HUILI &#61; 3 , SHOES_TYPE_ERROR &#61; - 1 } eShoesType; class ShoesBase { public : ShoesBase ( ) { cout << "ShoesBase() called" << endl; } virtual ~ ShoesBase ( ) { cout << "~ShoesBase() called" << endl; } virtual void ShowTag ( ) &#61; 0 ; private : int _shoesBase; } ; class NikeShoes : public ShoesBase{ public : NikeShoes ( ) { cout << "NikeShoes() called" << endl; } virtual ~ NikeShoes ( ) { cout << "~NikeShoes() called" << endl; } void ShowTag ( ) override { cout << "NikeShoes::ShowTag() called" << endl; } private : } ; class AdidasShoes : public ShoesBase{ public : AdidasShoes ( ) { cout << "AdidasShoes() called" << endl; } virtual ~ AdidasShoes ( ) { cout << "~AdidasShoes() called" << endl; } void ShowTag ( ) override { cout << "AdidasShoes::ShowTag() called" << endl; } } ; class HuiLiShoes : public ShoesBase{ public : HuiLiShoes ( ) { cout << "HuiLiShoes() called" << endl; } virtual ~ HuiLiShoes ( ) { cout << "~HuiLiShoes() called" << endl; } void ShowTag ( ) override { cout << "HuiLiShoes::ShowTag() called" << endl; } } ; class ShoesFactory { public : ShoesFactory ( ) { cout << "ShoesFactory() called" << endl; } virtual ~ ShoesFactory ( ) { cout << "~ShoesFactory() called" << endl; } std:: shared_ptr< ShoesBase> CreateShoes ( eShoesType shoesType) ; } ; #endif
examStu.cpp
#include #include #include #include "examStu.h" using namespace std; std:: shared_ptr< ShoesBase> ShoesFactory:: CreateShoes ( eShoesType shoesType) { cout << "create sheoes class" << endl; std:: shared_ptr< ShoesBase> pShoes &#61; nullptr ; switch ( shoesType) { case SHOES_TYPE_NIKE: cout << "create nike class " << endl; pShoes &#61; std:: make_shared< NikeShoes> ( ) ; break ; case SHOES_TYPE_ADIDAS: cout << "create AdidasShoes class" << endl; pShoes &#61; std:: make_shared< AdidasShoes> ( ) ; break ; case SHOES_TYPE_HUILI: cout << "create HuiLiShoes class" << endl; pShoes &#61; std:: make_shared< HuiLiShoes> ( ) ; break ; default : cout << "create shoes type not found!!" << endl; break ; } if ( pShoes &#61;&#61; nullptr ) { cout << "create shoes class error!!" << endl; } return pShoes; }
main.cpp
#include #include #include #include "examStu.h" using namespace std; int main ( int argc, char * argv[ ] ) { ShoesFactory shoesFactory; std:: shared_ptr< ShoesBase> pHuiLishoes &#61; shoesFactory. CreateShoes ( SHOES_TYPE_HUILI) ; pHuiLishoes- > ShowTag ( ) ; std:: shared_ptr< ShoesBase> pNikeshoes &#61; shoesFactory. CreateShoes ( SHOES_TYPE_NIKE) ; pNikeshoes- > ShowTag ( ) ; std:: shared_ptr< ShoesBase> pAdidasshoes &#61; shoesFactory. CreateShoes ( SHOES_TYPE_ADIDAS) ; pAdidasshoes- > ShowTag ( ) ; return 0 ; }