Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16653
  • 博文数量: 8
  • 博客积分: 206
  • 博客等级: 入伍新兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 20:40
文章分类

全部博文(8)

文章存档

2012年(8)

我的朋友

分类: C/C++

2012-06-03 20:54:35

看了一些别的工厂示例,挺不靠谱, 这个是比较使用的一个, 用了容器map,  GetInstance可以传入一些有用参数, 好久没写c++ code了,  被sll带到沟里了, :P

点击(此处)折叠或打开

  1. #ifndef LOGICFUNCCALC_H
  2. #define LOGICFUNCCALC_H

  3. #include <iostream>
  4. #include <map>
  5. #include <string>

  6. class LogicFuncCalcBase;
  7. typedef LogicFuncCalcBase* (*FactoryFunction)(int );
  8. class LogicFuncCalcFactory
  9. {
  10. public:
  11.     static void Register(std::string name, FactoryFunction instanceFunction)
  12.     {
  13.         m_FactoryFunctions[name] = instanceFunction;
  14.     }
  15.     static LogicFuncCalcBase* GetInstance(std::string name, int inputnum=1)
  16.     {
  17.         if (m_FactoryFunctions.count(name))
  18.         {
  19.             return m_FactoryFunctions[name](inputnum);
  20.         }
  21.         else
  22.         {
  23.             return NULL;
  24.         }
  25.     }
  26. private:
  27.     static std::map<std::string, FactoryFunction> m_FactoryFunctions;
  28. };


  29. class LogicFuncCalcBase
  30. {
  31. public:
  32.     virtual char calculate(char * input) = 0;
  33. };

  34. class NotCalc : public LogicFuncCalcBase
  35. {
  36. public:
  37.     
  38.     NotCalc(int inputnum=1):m_inputnum(1)
  39.     {
  40.         if (inputnum != 1)
  41.         {
  42.             std::cout << "input can only be 1!So it is changed to !" << std::endl;
  43.         }
  44.     }

  45.     char calculate(char * input)
  46.     {

  47.         //std::cout << "not calculated!" << std::endl;
  48.         if (input[0] == 'f' ){
  49.             return 't';
  50.         }
  51.         return 'f';
  52.     }

  53.     static LogicFuncCalcBase* CreateInstance(int inputnum=1)
  54.     {
  55.         return new NotCalc(inputnum);
  56.     }

  57. private:
  58.     const int m_inputnum;
  59. };

  60. class AndCalc : public LogicFuncCalcBase
  61. {
  62. public:
  63.     
  64.     AndCalc(int inputnum=1):m_inputnum(inputnum){}

  65.     char calculate(char * input)
  66.     {
  67.         //std::cout << "and calculated!" << std::endl;
  68.         for (int i=0;i<m_inputnum;i++){
  69.             if(input[i]=='f'){
  70.                 return 'f';    
  71.             }    
  72.         }
  73.         return 't';
  74.     }
  75.     static LogicFuncCalcBase* CreateInstance(int inputnum=1)
  76.     {
  77.         return new AndCalc(inputnum);
  78.     }

  79. private:
  80.     const int m_inputnum;
  81. };

  82. class OrCalc : public LogicFuncCalcBase
  83. {
  84. public:
  85.     
  86.     OrCalc(int inputnum=1):m_inputnum(inputnum){}

  87.     char calculate(char * input)
  88.     {
  89.         //std::cout << "or calculated!" << std::endl;
  90.         for (int i=0;i<m_inputnum;i++){
  91.             if(input[i]=='t'){
  92.                 return 't';    
  93.             }    
  94.         }
  95.         return 'f';
  96.     }

  97.     static LogicFuncCalcBase* CreateInstance(int inputnum=1)
  98.     {
  99.         return new OrCalc(inputnum);
  100.     }

  101. private:
  102.     const int m_inputnum;
  103. };

  104. class XorCalc : public LogicFuncCalcBase
  105. {
  106. public:
  107.     
  108.     XorCalc(int inputnum=1):m_inputnum(inputnum){}

  109.     char calculate(char * input)
  110.     {
  111.         //std::cout << "xor calculated!" << std::endl;
  112.         bool res = false;
  113.         for (int i=0;i<m_inputnum;i++){
  114.             if(input[i]=='t'){
  115.                 res^=true;    
  116.             }else{
  117.                 res^=false;
  118.             }
  119.         }
  120.         if (res == true) {
  121.             return 't';
  122.         }
  123.         return 'f';
  124.     }

  125.     static LogicFuncCalcBase* CreateInstance(int inputnum=1)
  126.     {
  127.         return new XorCalc(inputnum);
  128.     }

  129. private:
  130.     const int m_inputnum;
  131. };

  132. /*
  133. int main()
  134. {
  135.  LogicFuncCalcFactory::Register("not", &NotCalc::CreateInstance);
  136.  LogicFuncCalcFactory::Register("and", &AndCalc::CreateInstance);
  137.  LogicFuncCalcFactory::Register("or", &OrCalc::CreateInstance);
  138.  LogicFuncCalcFactory::Register("xor", &XorCalc::CreateInstance);

  139.  DeviceBase* pShape = NULL;
  140.  pShape = DeviceFactory::GetInstance("singleton");
  141.  if (NULL == pShape)
  142.  {
  143.   std::cout << "Can't find the produce in the factory!" << std::endl;
  144.  }
  145.  else
  146.  {
  147.   pShape->Draw();
  148.   delete pShape;
  149.   pShape = NULL;
  150.  }
  151.  return 0;
  152. }
  153. */
  154. #endif // LOGICFUNCCALC_H


阅读(485) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:独家记忆

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

Aquester2012-06-04 09:45:12

这个例子是工厂方法 吧?

评论热议
请登录后评论。

登录 注册