工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,也就是说工厂方法模式让实例化推迟到子类。
工厂方法模式非常符合“开闭原则”,当需要增加一个新的产品时,我们只需要增加一个具体的产品类和与之对应的具体工厂即可,无须修改原有系统。同时在工厂方法模式中用户只需要知道生产产品的具体工厂即可,无须关系产品的创建过程,甚至连具体的产品类名称都不需要知道。虽然他很好的符合了“开闭原则”,但是由于每新增一个新产品时就需要增加两个类,这样势必会导致系统的复杂度增加。
具体代码如下
#include
using namespace std;
class SingleCore
{
public:
virtual void Show() = 0;
};
class SingleCoreA:public SingleCore
{
public:
void Show()
{
cout << "Single Core A" << endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void Show()
{
cout << "Single Core B" << endl;
}
};
class Factory
{
public:
virtual SingleCore* CreateSingleCore() = 0;
};
class FactoryA:public Factory
{
public:
SingleCore* CreateSingleCore()
{
cout << "Create SingleCore A" << endl;
return new SingleCoreA();
}
};
class FactoryB:public Factory
{
public:
SingleCore* CreateSingleCore()
{
cout << "Create SingleCore B" << endl;
return new SingleCoreB();
}
};
int main()
{
FactoryA m;
SingleCore* p = m.CreateSingleCore();
p->Show();
FactoryB n;
p = n.CreateSingleCore();
p->Show();
}
缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,
在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事
抽象工厂模式是指定义一个创建一些列相关或相互依赖的对象接口,而无需指定它们具体的类。
例如:这家公司的技术不断发展,现在可以生产多核的处理器,具体方法:还是开设两个工厂分别用来生产A,B两种型号的处理器。
具体代码如下:
#include
using namespace std;
class SingleCore
{
public:
virtual void Show() = 0;
};
class SingleCoreA:public SingleCore
{
public:
void Show()
{
cout << " SingleCoreA"<<endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void Show()
{
cout << " SingleCoreA"<<endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void Show()
{
cout << "SingleCoreB" << endl;
}
};
class MultiCore
{
public:
virtual void Show() = 0;
};
class MultiCoreA:public MultiCore
{
public:
void Show()
{
cout << "MultiCoreA" << endl;
}
};
class MultiCoreB:public MultiCore
{
public:
void Show()
{
cout << "MultiCoreB" << endl;
}
};
class CoreFactory
{
public:
virtual SingleCore* createSingleCore() = 0;
virtual MultiCore* createMultiCore() = 0;
};
class CoreFactoryA:public CoreFactory
{
public:
SingleCore* createSingleCore()
{
cout << "create SingleCoreA" << endl;
return new SingleCoreA();
}
MultiCore* createMultiCore()
{
cout << "create MultiCoreA" << endl;
return new MultiCoreA();
}
};
class CoreFactoryB:public CoreFactory
{
public:
SingleCore* createSingleCore()
{
cout << "create SingleCoreB" << endl;
return new SingleCoreB();
}
MultiCore* createMultiCore()
{
cout << "create MultiCoreB" << endl;
return new MultiCoreB();
}
};
int main()
{
CoreFactoryB Fcb;
SingleCore* core = Fcb.createSingleCore();
MultiCore* Mcore = Fcb.createMultiCore();
core->Show();
Mcore->Show();
CoreFactoryA Fca;
core = Fca.createSingleCore();
Mcore = Fca.createMultiCore();
core->Show();
Mcore->Show();
}
简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
例如:有一家生产处理核的厂家,它只有一个工厂,能够生产两种型号的处理器核。客户需要什么样的处理器,一定要显示地告诉生产工厂。
缺点: 每增加一个新的产品就需要增加一个对象的工厂
具体代码如下:
#include
using namespace std;
class SingleCore
{
public:
virtual void Show() = 0;
};
class SingleCoreA:public SingleCore
{
public:
void Show()
{
cout << "Single Core A" << endl;
}
};
class SingleCoreB:public SingleCore
{
public:
void Show()
{
cout << "Single Core B" << endl;
}
};
class Factory
{
public:
virtual SingleCore* CreateSingleCore() = 0;
};
class FactoryA:public Factory
{
public:
SingleCore* CreateSingleCore()
{
cout << "Create SingleCore A" << endl;
return new SingleCoreA();
}
};
class FactoryB:public Factory
{
public:
SingleCore* CreateSingleCore()
{
cout << "Create SingleCore B" << endl;
return new SingleCoreB();
}
};
int main()
{
FactoryA m;
SingleCore* p = m.CreateSingleCore();
p->Show();
FactoryB n;
p = n.CreateSingleCore();
p->Show();
}