工厂方法模式

动机(Motivation)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-31u4hOR0-1591236579185)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1591236473319.png)]

之前文件分割器demo:

版本1:

FileSplitter1.cpp

class FileSplitter{
public:
    void split(){
        //...
    }
};

MainForm1.cpp

class MainForm : public Form{
	TestBox* txtFilePath;
    TestBox* txtFileNumber;
    
public:
    void Button1_click(){
        string filePath = txtFilePath->getText();
        int number = atio(txtFileNumber->getText().c_str());
        
        FileSplitter* splitter = new FileSplitter(filePath, number);  //依赖具体类 不稳定
        splitter->split();
    }
};

版本2:

FileSplitter1.cpp

class ISplitter{ //抽象基类
public:
    virtual void split() = 0;
    virtual ~ISplitter(){}
};

class BinarySplitter : public ISplitter{

};

class TxtSplitter : public ISplitter{
    
};

class PicterSplitter : public ISplitter{
    
};

class VideoSplitter : public ISplitter{
    
};

MainForm1.cpp

class MainForm : public Form{
	TestBox* txtFilePath;
    TestBox* txtFileNumber;
    
public:
    void Button1_click(){
        string filePath = txtFilePath->getText();
        int number = atio(txtFileNumber->getText().c_str());
        
        ISplitter* splitter = //依赖抽象类 稳定
            new BinarySplitter(filePath, number); //依赖具体类 不稳定 问题!
        splitter->split();
    }
};

版本3:

SplitterFactory.cpp

class SplitterFactory{
public:
    ISplitter* CreateSplitter{
        return new BinarySplitter(); //具体类 有问题
    }
};

class ISplitter{ //抽象基类
public:
    virtual void split() = 0;
    virtual ~ISplitter(){}
};

MainForm2.cpp

class MainForm : public Form{
	TestBox* txtFilePath;
    TestBox* txtFileNumber;
    
public:
    void Button1_click(){
        
        SplitterFactory factory;        
        
        ISplitter* splitter = //依赖抽象类 稳定
            factory.CreateSplitter();
        splitter->split();
    }
};

版本4:

SplitterFactory.cpp

class SplitterFactory{
public:
    virtual ISplitter* CreateSplitter() = 0;
    virtual ~SplitterFactory(){}
};

class ISplitter{ //抽象基类
public:
    virtual void split() = 0;
    virtual ~ISplitter(){}
};

MainForm2.cpp

class MainForm : public Form{
	TestBox* txtFilePath;
    TestBox* txtFileNumber;
    
public:
    void Button1_click(){
        
        SplitterFactory* factory;  //?    
        
        ISplitter* splitter = //依赖抽象类 稳定
            factory->CreateSplitter();
        splitter->split();
    }
};

版本5:

FileSplitter2.cpp


//具体类
class BinarySplitter : public ISplitter{

};

class TxtSplitter : public ISplitter{
    
};

class PicterSplitter : public ISplitter{
    
};

class VideoSplitter : public ISplitter{
    
};

//具体工厂
class BinarySplitterFactory : public Splitter{
public:
    virtual ISplitter* CreateSplitter() {
        return new BinarySplitter();
    }
};

class TxtSplitterFactory : public Splitter{
public:
    virtual ISplitter* CreateSplitter() {
        return new TxtSplitter();
    }
};

class PictureSplitterFactory : public Splitter{
public:
    virtual ISplitter* CreateSplitter() {
        return new PictureSplitter();
    }
};

class VidepSplitterFactory : public Splitter{
public:
    virtual ISplitter* CreateSplitter() {
        return new VideoSplitter();
    }
};

MainForm2.cpp

class MainForm : public Form{
	
    SplitterFactory* factory;   //工厂
public:
    MainForm(SplitterFactory* factory){ //外部传递进来
        this->factory = factory;
    }
    
    void Button1_click(){
                
        
        ISplitter* splitter = //依赖抽象类 稳定
            factory->CreateSplitter(); //多态new
        splitter->split();
    }
};

SplitterFactory.cpp

class SplitterFactory{
public:
    virtual ISplitter* CreateSplitter() = 0;
    virtual ~SplitterFactory(){}
};

class ISplitter{ //抽象基类
public:
    virtual void split() = 0;
    virtual ~ISplitter(){}
};

模式定义:

定义一个用于创建对象的接口(SplitterFactory),让子类(BinarySplitterFactory)决定实例化哪一个类。 Factory Method使得一个类的实例化延迟(目的:解耦, 手段:虚函数)到子类。

​ 《设计模式》GoF

结构:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VzWM2h4W-1591236579189)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1591229535131.png)]

要点总结:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdHJrq83-1591236579191)(C:\Users\WGJ\AppData\Roaming\Typora\typora-user-images\1591229998736.png)]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值