• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
onlyan
#define true false                  
博客园    首页    新随笔    联系   管理    订阅  订阅

工厂模式(Factory)

工厂模式定义一个用于创建对象的接口,让它的子类决定实例化哪一个产品类。工厂方法使一个类的实例化延迟到其子类。

工厂方法实现时,客户端需要决定实例化哪一个具体工厂从而得到具体的产品类。

如果增加新产品,则需要相应增加一个新的工厂来生产这个产品。

Product.h
 1 //Product.h
 2 
 3 #ifndef _PRODUCT_H_
 4 #define _PRODUCT_H_
 5 
 6 class Product    //产品基类
 7 {
 8 public:
 9     virtual ~Product() = 0;
10 
11 protected:
12     Product();
13 
14 private:
15 };
16 
17 class ConcreteProductA: public Product    //具体产品A
18 {
19 public:
20     ConcreteProductA();
21     ~ConcreteProductA();
22 
23 protected:
24 
25 private:
26 };
27 
28 class ConcreteProductB: public Product    //具体产品B
29 {
30 public:
31     ConcreteProductB();
32     ~ConcreteProductB();
33 
34 protected:
35 
36 private:
37 };
38 
39 #endif
Product.cpp
 1 //Product.cpp
 2 
 3 #include "Product.h"
 4 #include <iostream>
 5 using namespace std;
 6 
 7 Product::Product()
 8 {
 9 
10 }
11 
12 Product::~Product()
13 {
14 
15 }
16 
17 ConcreteProductA::ConcreteProductA()
18 {
19     cout << "Concrete Product A..." << endl;
20 }
21 
22 ConcreteProductA::~ConcreteProductA()
23 {
24 
25 }
26 
27 ConcreteProductB::ConcreteProductB()
28 {
29     cout << "Concrete Product B..." << endl;
30 }
31 
32 ConcreteProductB::~ConcreteProductB()
33 {
34 
35 }
Factory.h
 1 //Factory.h
 2 
 3 #ifndef _FACTORY_H_
 4 #define _FACTORY_H_
 5 
 6 class Product;
 7 
 8 class Factory    //工厂基类
 9 {
10 public:
11     virtual ~Factory() = 0;
12     virtual Product* CreateProduct() = 0;
13 
14 protected:
15     Factory();
16 
17 private:
18 };
19 
20 class ConcreteFactoryA: public Factory    //具体工厂A,用于生产产品A
21 {
22 public:
23     ~ConcreteFactoryA();
24     ConcreteFactoryA();
25     Product* CreateProduct();
26 
27 protected:
28     
29 private:
30 };
31 
32 class ConcreteFactoryB: public Factory    //具体工厂B,用于生产产品B
33 {
34 public:
35     ~ConcreteFactoryB();
36     ConcreteFactoryB();
37     Product* CreateProduct();
38 
39 protected:
40 
41 private:
42 };
43 
44 #endif
Factory.cpp
 1 //Factory.cpp
 2 
 3 #include "Factory.h"
 4 #include "Product.h"
 5 #include <iostream>
 6 using namespace std;
 7 
 8 Factory::Factory()
 9 {
10 
11 }
12 
13 Factory::~Factory()
14 {
15 
16 }
17 
18 ConcreteFactoryA::ConcreteFactoryA()
19 {
20     cout << "Concrete Factory A..." << endl;
21 }
22 
23 ConcreteFactoryA::~ConcreteFactoryA()
24 {
25 
26 }
27 
28 Product* ConcreteFactoryA::CreateProduct()
29 {
30     return new ConcreteProductA();
31 }
32 
33 ConcreteFactoryB::ConcreteFactoryB()
34 {
35     cout << "Concrete Factory B..." << endl;
36 }
37 
38 ConcreteFactoryB::~ConcreteFactoryB()
39 {
40 
41 }
42 
43 Product* ConcreteFactoryB::CreateProduct()
44 {
45     return new ConcreteProductB();
46 }
main.cpp
 1 //main.cpp
 2 
 3 #include "Factory.h"
 4 #include "Product.h"
 5 #include <iostream>
 6 using namespace std;
 7 
 8 int main(int argc, char* argv[])
 9 {
10     Factory* factory;
11     Product* product;
12 
13     factory = new ConcreteFactoryA();
14     product = factory->CreateProduct();
15 
16     factory = new ConcreteFactoryB();
17     product = factory->CreateProduct();
18 
19     return 0;
20 }

简单工厂模式相对于工厂模式的优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。但如果需要添加新产品,就要修改工厂类,这样违背了开放-封闭原则。

posted @ 2012-06-30 21:43  onlyan  阅读(216)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3