工厂模式
作者:互联网
工厂模式
定义一个用于创建对象的接口,让子类决定实例化哪一个类,Factory Method使得一个类的实例化延迟(目的:解耦,手段:虚函数)到子类。
class SplitterFactory
{
public:
SplitterFactory(){ }
virtual ~SplitterFactory(){ }
virtual Splitter* createSplitter() = 0;
}
class Splitter
{
public:
Splitter(){ }
virtual ~Splitter(){ }
virtual void split() = 0;
}
class TextSplitterFactory : public SplitterFactory
{
public:
virtual Splitter* createSplitter() overrive
{
Splitter* splitter=new TextSplitter();
//...
return splitter;
}
}
class TextSplitter : public Splitter
{
virtual void split() overrive
{
//...
}
}
class MainForm
{
public:
MainForm(SplitterFactory* factory):factory_(factory){}
~MainForm(){}
void buttonClick()
{
Splitter* splitter = factory->createSplitter();
splitter->split();
}
private:
SplitterFactory* factory_;
}
UML类图
标签:Splitter,class,factory,virtual,工厂,SplitterFactory,模式,public 来源: https://www.cnblogs.com/shiguoliang/p/15366768.html