反射机制

反射这个特性在C++中是没有的。所谓反射,自己的认为就是通过一个名字就可创建、调用、获取信息等一系列的操作,这个在脚本语言里面是比较常见的,COM组件也类似,知道个ID名,就可以做很多的工作了。

下面提供一种反射技术的实现(C++):

 UML关系:

常见的关系有:继承(Inheritance),关联关系(Association),聚合关系(Aggregation),复合关系(Composition),依赖关系(Dependency)。
其中,聚合关系(Aggregation),复合关系(Composition)属于关联关系(Association)。
一般关系表现为继承或实现关系(is a),关联关系表现为变量(has a ),依赖关系表现为函数中的参数(use a)。

 

                                             UML图

  1 //--反射技术的实现(C++)
  2 //////////////////////////////////////////////////////////////////////////////////////////
  3 #ifndef __REFLECTIONFACTORY_H__
  4 #define __REFLECTIONFACTORY_H__
  5 
  6 #include<iostream>
  7 #include<string>
  8 #include<map>
  9 using namespace std;
 10 
 11 //应用(产品)接口类
 12 class Base
 13 {
 14 public:
 15     Base(){m_className="Base";}
 16     virtual ~Base(){}
 17     virtual void me(){ cout<<"me is :"<<m_className<<endl;}
 18     
 19 public:
 20     string m_className;
 21 };
 22 
 23 //---------------------------------------------------------------------------------------
 24 //反射工厂类(抽象工厂模式)
 25 class ReflectionFactory
 26 {
 27 public:
 28     //反射技术,根据类名实例化对应类 进行调用
 29     static Base * GetProcedure(const string & name);
 30     static int    ExecuteProcedure(const string &name);
 31     //不带参数方式执行,即。直接调用
 32     static int    ExecuteProcedure(Base* pProc);
 33     
 34 public:
 35     static int    SetRelationMapping(const string &name,Base * procedure);
 36     /*here,名称串和已实例化类指针的对应关系,这儿为甚不用变量而用函数来实现变量!?
 37       是因为接下来的宏注册!!!!(深层原因???--宏注册的变量定义是在静态成员变量定义之前?)
 38     */
 39     static        map<string,Base*> *GetRelationMapping();
 40 };
 41 
 42 #define INSTALL_PROCESSBASE_INSTANCE(classname) \
 43     int classname##Suffix = ReflectionFactory::SetRelationMapping(#classname,new classname());
 44 
 45 #endif //__REFLECTIONFACTORY_H__
 46 
 47 //////////////////////////////////////////////////////////////////////////////////////////
 48 //反射工厂类的实现
 49 map<string,Base*> * ReflectionFactory::GetRelationMapping() 
 50 {
 51     //局部静态变量:函数体内如果在定义静态变量的同时进行了初始化,则以后程序不再进行初始化操作(出现在函数内部的基本类型的的静态变量初始化语句只有在第一次调用才执行)。
 52     static map<string,Base*> relationMapping;
 53     return &relationMapping;
 54 }
 55 
 56 Base * ReflectionFactory::GetProcedure(const string & name) 
 57 {
 58     map<string,Base*> &procs =  *(GetRelationMapping());
 59     
 60     map<string,Base*>::iterator itr;
 61     itr=procs.find(name);
 62     if (itr != procs.end()) 
 63     {
 64         return itr->second;  
 65     }
 66     return NULL;          
 67 }
 68 
 69 int ReflectionFactory::SetRelationMapping(const string &name,Base * procedure) 
 70 {
 71     map<string,Base*> &procs =  *(GetRelationMapping());
 72     Base * p =GetProcedure(name);
 73     if (NULL == p)  {
 74         procs[name] = procedure;
 75     }
 76     procedure->m_className = name;
 77     
 78 #ifdef _DEBUG_
 79     cout << "register class instance:" << name << endl;
 80 #endif
 81     
 82     return 0;
 83 }
 84 
 85 int ReflectionFactory::ExecuteProcedure(const string &name) 
 86 {
 87     int iRet=0 ;
 88     string proc_name = name;
 89     Base *proc = ReflectionFactory::GetProcedure(proc_name);
 90     
 91     if (NULL == proc)
 92     {iRet = 9;}
 93     else
 94     {iRet = ExecuteProcedure(proc);}
 95     
 96 #ifdef _DEBUG_
 97     cout << "Executed Procedure:" << proc->m_className << endl; 
 98 #endif
 99     
100     return iRet;
101 }
102 
103 int ReflectionFactory::ExecuteProcedure(Base* pProc)
104 {
105     pProc->me();
106     return 0;
107 }
108 //////////////////////////////////////////////////////////////////////////////////////////
109 //应用(产品)类的定义
110 class A1 :public Base
111 { 
112 };
113 class A2 :public Base
114 { 
115 };
116 class A3 :public Base
117 { 
118 };
119 
120 //////////////////////////////////////////////////////////////////////////////////////////
121 //注册类名称和其对应实例化指针的Mapping:
122 INSTALL_PROCESSBASE_INSTANCE(A2);
123 INSTALL_PROCESSBASE_INSTANCE(A3);
124 INSTALL_PROCESSBASE_INSTANCE(Base);
125 
126 //////////////////////////////////////////////////////////////////////////////////////////
127 //根据类名来实例化和调用产品类,样例:
128 int main()
129 {
130     map<string,Base*> &procs =  *(ReflectionFactory::GetRelationMapping());
131     cout<<"ReflectionFactory::size(): "<<procs.size()<<endl;
132     
133     string l_className="Base";
134     cout<<l_className.c_str()<<endl;
135     int t_ret = ReflectionFactory::ExecuteProcedure(l_className);
136         
137     l_className="A2";
138     cout<<l_className.c_str()<<endl;
139     t_ret = ReflectionFactory::ExecuteProcedure(l_className);
140         
141     return t_ret;
142 }
Reflection.cpp

 该样例使用了工程模式、策略模式、反射机制,就差一个配置文件机制就O了...

posted on 2014-03-07 17:31  光标  阅读(322)  评论(0编辑  收藏  举报

导航