C++设计模式02——工厂方法模式
2017-12-30 22:04
253 查看
参考书:《大话设计模式》和《设计模式:可复用面向对象软件的基础》
1.意图
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
2.别名
虚构造器(Virtual Constructor )
3.动机
框架使用抽象类定义和维护对象之间的关系。这些对象的创建通常也由框架负责。
4.适用性
在下列情况下可以使用Factory Method模式:
a)当一个类不知道它所必须创建的对象的类的时候
b)当一个类希望由它的子类来指定所创建的对象的时候。
c)当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候
5.结构
#include <iostream>
using namespace std;
class Product
{
public:
virtual void Show() = 0;
virtual ~Product()
{
}
};
class ProductA: public Product
{
public:
void Show()
{
cout << "I'm Product=================A" << endl;
}
};
class ProductB: public Product
{
public:
void Show()
{
cout << "I'm Product=================B" << endl;
}
};
class ProductC: public Product
{
public:
void Show()
{
cout << "I'm Product=================C" << endl;
}
};
class Factory
{
public:
virtual Product *CreateProduct() = 0;
virtual ~Factory()
{
}
};
class FactoryA: public Factory
{
public:
Product *CreateProduct()
{
return new ProductA();
}
};
class FactoryB: public Factory
{
public:
Product *CreateProduct()
{
return new ProductB();
}
};
class FactoryC: public Factory
{
public:
Product *CreateProduct()
{
return new ProductC();
}
};
int main()
{
Factory *factoryA = new FactoryA();
Product *productA = factoryA->CreateProduct();
productA->Show();
Factory *factoryB = new FactoryB();
Product *productB = factoryB->CreateProduct();
productB->Show();
Factory *factoryC = new FactoryC();
Product *productC = factoryC->CreateProduct();
productC->Show();
if(NULL != factoryA)
{
delete factoryA;
factoryA = NULL;
}
if(NULL != productA)
{
delete productA;
productA = NULL;
}
if(NULL != factoryB)
{
delete factoryB;
factoryB = NULL;
}
if(NULL != productB)
{
delete productB;
productB = NULL;
}
if(NULL != factoryC)
{
delete factoryC;
factoryC = NULL;
}
if(NULL != productC)
{
delete productC;
productC = NULL;
}
cout << "Hello World!" << endl;
return 0;
}
1.意图
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
2.别名
虚构造器(Virtual Constructor )
3.动机
框架使用抽象类定义和维护对象之间的关系。这些对象的创建通常也由框架负责。
4.适用性
在下列情况下可以使用Factory Method模式:
a)当一个类不知道它所必须创建的对象的类的时候
b)当一个类希望由它的子类来指定所创建的对象的时候。
c)当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候
5.结构
#include <iostream>
using namespace std;
class Product
{
public:
virtual void Show() = 0;
virtual ~Product()
{
}
};
class ProductA: public Product
{
public:
void Show()
{
cout << "I'm Product=================A" << endl;
}
};
class ProductB: public Product
{
public:
void Show()
{
cout << "I'm Product=================B" << endl;
}
};
class ProductC: public Product
{
public:
void Show()
{
cout << "I'm Product=================C" << endl;
}
};
class Factory
{
public:
virtual Product *CreateProduct() = 0;
virtual ~Factory()
{
}
};
class FactoryA: public Factory
{
public:
Product *CreateProduct()
{
return new ProductA();
}
};
class FactoryB: public Factory
{
public:
Product *CreateProduct()
{
return new ProductB();
}
};
class FactoryC: public Factory
{
public:
Product *CreateProduct()
{
return new ProductC();
}
};
int main()
{
Factory *factoryA = new FactoryA();
Product *productA = factoryA->CreateProduct();
productA->Show();
Factory *factoryB = new FactoryB();
Product *productB = factoryB->CreateProduct();
productB->Show();
Factory *factoryC = new FactoryC();
Product *productC = factoryC->CreateProduct();
productC->Show();
if(NULL != factoryA)
{
delete factoryA;
factoryA = NULL;
}
if(NULL != productA)
{
delete productA;
productA = NULL;
}
if(NULL != factoryB)
{
delete factoryB;
factoryB = NULL;
}
if(NULL != productB)
{
delete productB;
productB = NULL;
}
if(NULL != factoryC)
{
delete factoryC;
factoryC = NULL;
}
if(NULL != productC)
{
delete productC;
productC = NULL;
}
cout << "Hello World!" << endl;
return 0;
}
相关文章推荐
- c++设计模式—工厂方法模式
- 设计模式(Design Patterns)-可复用面向对象软件的基础 02:工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory)
- C++设计模式四--FactoryPattern(工厂方法模式)
- C++设计模式——工厂方法模式
- 设计模式02 -工厂方法模式
- 我所理解的设计模式(C++实现)——工厂方法模式(Factory Method Pattern)
- C++设计模式(4):工厂方法模式
- C++设计模式之工厂方法模式
- 设计模式---工厂方法模式(c++实现)
- c++设计模式之工厂方法模式
- C++设计模式之工厂方法模式(Factory Method)
- 设计模式 工厂方法模式 C++实现
- 设计模式-02-工厂方法模式的剖析
- Java与设计模式(02)--工厂方法模式
- 我所理解的设计模式(C++实现)——工厂方法模式(Factory Method Pattern)
- 设计模式之02工厂方法模式(笔记)
- C++设计模式之工厂方法模式(一)
- 设计模式C++实现(18)——工厂方法模式
- 设计模式系列02:工厂方法模式
- 设计模式之 工厂方法模式(C++实现)