首页 > 程序开发 > 软件开发 > 其他 >

工厂模式

2017-04-17

工厂模式:工厂里面只封装一个指针。

工厂模式:工厂里面只封装一个指针。

#include

using namespace std;

class Product{//功能基类

public:

Product()

{

cout<<"Product.com"< }

virtual void Print()

{

cout<<"Product.Print.com"< }

virtual ~Product()

{

cout<<"Product.des"< }

};

class ProductA:public Product{//功能A

public:

ProductA()

{

cout<<"ProductA.com"< }

void Print()

{

cout<<"ProductA.Print.com"< }

~ProductA()

{

cout<<"ProductA.des"< }

};

class ProductB:public Product{//功能B

public:

ProductB()

{

cout<<"ProductB.com"< }

void Print()

{

cout<<"ProductB.Print.com"< }

~ProductB()

{

cout<<"ProductB.des"< }

};

class ProductC:public Product{//功能C

public:

ProductC()

{

cout<<"ProductC.com"< }

void Print()

{

cout<<"ProductC.Print.com"< }

~ProductC()

{

cout<<"ProductC.des"< }

};

class Factory{//工厂基类

protected:

Product * A;

public :

Factory(Product * x=NULL)

{

A=x;

cout<<"Protory.com"< }

virtual void du()

{

cout<<"Factory.du"< }

virtual ~Factory()

{

delete A;

A=NULL;

cout<<"Factory.des"< }

};

class FactoryA:public Factory{//工厂A

public:

FactoryA()

{

A=new ProductA();

cout<<"FactoryA.com"< }

void du()

{

A->Print();

}

~FactoryA()

{

delete A;

A=NULL;

cout<<"FactoryA.des"< }

};

class FactoryB:public Factory{//工厂B

public:

FactoryB()

{

A=new ProductB();

cout<<"FactoryB.com"< }

void du()

{

A->Print();

}

~FactoryB()

{

delete A;

A=NULL;

cout<<"FactoryB.des"< }

};

class FactoryC:public Factory{//工厂C

public:

FactoryC()

{

A=new ProductC();

cout<<"FactoryC.com"< }

void du()

{

A->Print();

}

~FactoryC()

{

delete A;

A=NULL;

cout<<"FactoryC.des"< }

};

int main(void)

{

Factory * p=new FactoryB();

p->du();

delete p;

return 0;

}

相关文章
最新文章
热点推荐