Chinaunix首页 | 论坛 | 博客
  • 博客访问: 147879
  • 博文数量: 27
  • 博客积分: 531
  • 博客等级: 一等列兵
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-25 18:31
文章分类

全部博文(27)

文章存档

2015年(4)

2014年(3)

2013年(6)

2012年(14)

我的朋友

分类: Delphi

2012-05-19 16:04:03

    开始第一个实质性的博文,之前粗略的汇总了23中设计模式,下面该一一道来。
    首先就是这 工厂方法 (factory method)

    从简单工厂说起:
           1. 工厂类  Factory
           2. 工厂所能生产的产品的抽象 Product
                  2.1. 实际的产品 ProductA
                  2.2. 实际的产品 ProductB
                  2.3. ......
                  注:实际的产品都具备 Product 的基本功能 (即接口的实现)

           使用:用户那到工厂的实例,调用他的方法;传入参数 A 则生成的是 A 产品,B 则生成 B 产品;
                      而对用户来说,使用的方式都和 Product 一样,只是不同的产品最终的表现不同。
代码示例:
1.
factory.h

点击(此处)折叠或打开

  1. #ifndef _FACTORY_H_
  2. #define _FACTORY_H_

  3. #include "operation.h"

  4. class Factory
  5. {
  6. public:
  7.     Factory();
  8.     Operation* CreateOpteration(char* type);
  9. };

  10. #endif
2. factory.cpp

点击(此处)折叠或打开

  1. #include <cstring>
  2. #include "factory.h"
  3. #include "operation_add.h"
  4. #include "operation_sub.h"

  5. Factory::Factory()
  6. {
  7. }

  8. Operation* Factory::CreateOpteration(char* type)
  9. {
  10.     Operation* op = NULL;
  11.     if(0 == strcmp(type, "add"))
  12.     {
  13.         op = new OperationAdd();
  14.     }
  15.     else if (0 == strcmp(type, "sub"))
  16.     {
  17.         op = new OperationSub();
  18.     }
  19.     else
  20.     {
  21.         // error type
  22.     }
  23.     return op;
  24. }
3. operation.h

点击(此处)折叠或打开

  1. #ifndef _OPERATION_H_
  2. #define _OPERATION_H_

  3. class Operation
  4. {
  5. public:
  6.     Operation();
  7.     void setA(int num);
  8.     void setB(int num);
  9.     virtual int GetResult();

  10. protected:
  11.     int m_numA;
  12.     int m_numB;
  13. };

  14. #endif
4. operation.cpp

点击(此处)折叠或打开

  1. #include "operation.h"

  2. Operation::Operation()
  3. {
  4.     m_numA = 0;
  5.     m_numB = 0;
  6. }

  7. void Operation::setA(int num)
  8. {
  9.     m_numA = num;
  10. }

  11. void Operation::setB(int num)
  12. {
  13.     m_numB = num;
  14. }

  15. int Operation::GetResult()
  16. {
  17.     return 0;
  18. }
5. operation_add.h

点击(此处)折叠或打开

  1. #ifndef _OPERATION_ADD_H_
  2. #define _OPERATION_ADD_H_

  3. #include "operation.h"

  4. class OperationAdd : public Operation
  5. {
  6. public:
  7.     OperationAdd();
  8.     int GetResult();
  9. };

  10. #endif
6. operation_add.cpp

点击(此处)折叠或打开

  1. #include "operation_add.h"

  2. OperationAdd::OperationAdd()
  3. {
  4. }

  5. int OperationAdd::GetResult()
  6. {
  7.     return m_numA + m_numB;
  8. }
7. operation_sub.h

点击(此处)折叠或打开

  1. #ifndef _OPERATION_SUB_H_
  2. #define _OPERATION_SUB_H_

  3. #include "operation.h"

  4. class OperationSub : public Operation
  5. {
  6. public:
  7.     OperationSub();
  8.     int GetResult();
  9. };

  10. #endif
8. operation_sub.cpp

点击(此处)折叠或打开

  1. #include "operation_sub.h"

  2. OperationSub::OperationSub()
  3. {
  4. }

  5. int OperationSub::GetResult()
  6. {
  7.     return m_numA - m_numB;
  8. }
9. test.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "operation.h"
  3. #include "factory.h"

  4. int main(int argc, char* argv[])
  5. {
  6.     Operation* op = NULL;
  7.     Factory fa;
  8.     op = fa.CreateOpteration((char*)"add");
  9.     if(NULL != op)
  10.     {
  11.         op->setA(1);
  12.         op->setB(2);
  13.     }
  14.     std::cout << "1 add 2 : " << op->GetResult() << std::endl;

  15.     delete op;
  16.     op = NULL;
  17.     op = fa.CreateOpteration((char*)"sub");
  18.     if(NULL != op)
  19.     {
  20.         op->setA(1);
  21.         op->setB(2);
  22.     }
  23.     std::cout << "1 sub 2 : " << op->GetResult() << std::endl;
  24.     delete op;

  25.     return 0;
  26. }
       以上是一个简单工厂的实现,包括测测试代码。实现的功能:工厂可以产生 operation,至于最终产生哪种
operation,根据用户参数决定! (代码在 linux gcc 4.4.3 编译运行通过)

       通过上述代码发现,要是现在增加一种 operation 则需要修改 factory 中生成产品的方法的实现,即违背了
开放封闭原则(这也是简单工厂没有进入 23 中设计模式的原因),故有 工厂方法 出现。
       其实实质上,工厂方法 是将 简单工厂 中根据参数的判断扔给了用户;
      1. 产品 Product
          1.1. 产品 ProductA
          1.2. ......
      2. 创建着 Creator
          2.1. 创建者 CreatorA
          2.2. ......
      用户得到创建者 A 就可以创建出产品 A,......
代码示例:
1. product.h

点击(此处)折叠或打开

  1. #ifndef _PRODUCT_H_
  2. #define _PRODUCT_H_

  3. class Product
  4. {
  5. public:
  6.     virtual void operation() = 0;
  7. };

  8. #endif
2. creater.h

点击(此处)折叠或打开

  1. #ifndef _CREATER_H_
  2. #define _CREATER_H_

  3. #include "product.h"

  4. class Creater
  5. {
  6. public:
  7.     virtual Product* FactoryMethod() = 0;
  8. };

  9. #endif
3. productA.h

点击(此处)折叠或打开

  1. #ifndef _PRODUCT_A_H_
  2. #define _PRODUCT_A_H_

  3. #include "product.h"

  4. class ProductA : public Product
  5. {
  6. public:
  7.     void operation();
  8. };

  9. #endif
4. productA.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "productA.h"

  3. void ProductA::operation()
  4. {
  5.     std::cout << "ProductA - operation" << std::endl;
  6. }
5. createrA.h

点击(此处)折叠或打开

  1. #ifndef _CREATER_A_H_
  2. #define _CREATER_A_H_

  3. #include "product.h"
  4. #include "creater.h"

  5. class CreaterA : public Creater
  6. {
  7. public:
  8.     Product* FactoryMethod();
  9. };

  10. #endif
6. createrA.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "createrA.h"
  3. #include "productA.h"
  4. Product* CreaterA::FactoryMethod()
  5. {
  6.     std::cout << "CreaterA - FactoryMethod" << std::endl;
  7.     Product* pt = new ProductA();
  8.     return pt;
  9. }
7. test.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "product.h"
  3. #include "creater.h"
  4. #include "createrA.h"

  5. int main(int argc, char* argv[])
  6. {
  7.     Creater* ct = new CreaterA();
  8.     Product* pt = ct->FactoryMethod();

  9.     pt->operation();

  10.     delete ct;
  11.     delete pt;
  12. }
       上述是一个简单的 工厂方法 模式实现,非常的粗糙,见谅。
       由于在linux下,对画图工具还是清楚,故没有给出 UML 图,见谅 -_-!
       --- end ---

阅读(1894) | 评论(0) | 转发(0) |
0

上一篇:设计模式

下一篇:策略模式

给主人留下些什么吧!~~