动机(Motivation)
之前文件分割器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