工厂模式demo
工厂方法模式
#include <QCoreApplication>
#include <iostream>
using namespace std;class Product
{
public:virtual void show() = 0;
};class Phone:public Product
{
public:void show(){cout << "生产手机" << endl;}
};class Tv:public Product
{
public:void show(){cout << "生产电视" << endl;}
};class Factory
{
public:virtual Product* newProduct() = 0;
};class PhoneFactory: public Factory
{
public:Product* newProduct(){cout << "手机事业部生产手机" << endl;return new Phone();}
};class TvFactory:public Factory
{
public:Product* newProduct(){cout << "电视事业部生产电视" << endl;return new Tv();}
};int main(int argc, char *argv[])
{Factory* tvFactory = new TvFactory();Product* tv = tvFactory->newProduct();Factory* phoneFactory = new PhoneFactory();Product* phone = phoneFactory->newProduct();delete tv;delete tvFactory;delete phone;delete phoneFactory;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}
抽象工厂模式
#include <QCoreApplication>
#include <iostream>
#include <string>using namespace std;
class Phone
{
public:virtual string CreatPhone() = 0;
};class Tv
{
public:virtual string CreatTv() = 0;
};class NumberPhone : public Phone
{
public:string CreatPhone() override{return "小码数字系列手机";}
};class GQTv : public Tv
{
public:string CreatTv() override{return "小码高清电视";}
};class WomanPhone : public Phone
{
public:string CreatPhone() override{return "小花女士手机";}
};class WomanTv : public Tv
{
public:string CreatTv() override{return "小花女士电视";}
};class AbstractFactory
{
public:virtual Phone* GetPhone() = 0;virtual Tv* GetTv() = 0;
};class XMFactory : public AbstractFactory
{
public:Phone* GetPhone() override{return new NumberPhone();}Tv* GetTv() override{return new GQTv();}
};class BDFactory : public AbstractFactory
{
public:Phone* GetPhone() override{return new WomanPhone();}Tv* GetTv() override{return new WomanTv();}
};
using namespace std;
int main(int argc, char *argv[])
{XMFactory *xmFactory = new XMFactory();auto phone = xmFactory->GetPhone()->CreatPhone();auto tv = xmFactory->GetTv()->CreatTv();cout << "Phone : " << phone << "Tv : " << tv << endl;BDFactory *dbFactry = new BDFactory();phone = dbFactry->GetPhone()->CreatPhone();tv = dbFactry->GetTv()->CreatTv();cout << "Phone : " << phone << "Tv : " << tv << endl;delete xmFactory;delete dbFactry;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}
简单工厂模式
#include <QCoreApplication>
#include <iostream>
using namespace std;typedef enum PhoneType
{ManPhone,WomanPhone,ChildPhone
}PHONE;class Phone
{
public:virtual void Sale() = 0;
};class PhoneMan: public Phone
{void Sale(){cout << "Sale Man Phone" << endl;}
};class PhoneWoman: public Phone
{void Sale(){cout << "Sale Woman Phone" << endl;}
};class PhoneChild: public Phone
{void Sale(){cout << "Sale child Phone" << endl;}
};class Factory
{
public:Factory(){}Phone* CreatePhone(PHONE type){switch(type){case ManPhone:return new PhoneMan();case WomanPhone:return new PhoneWoman();case ChildPhone :return new PhoneChild();default:return NULL;}}
};
int main(int argc, char *argv[])
{Factory *PhoneFactory = new Factory();Phone *manPhone = PhoneFactory->CreatePhone(ManPhone);if(manPhone != NULL)manPhone->Sale();Phone* womanPhone = PhoneFactory->CreatePhone(WomanPhone);if(womanPhone != NULL)womanPhone->Sale();Phone* childPhone = PhoneFactory->CreatePhone(ChildPhone);if(childPhone != NULL)childPhone->Sale();delete PhoneFactory;delete manPhone;delete womanPhone;delete childPhone;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}