从space.live.com上搬过来,担心过两天就没了。
关于AOP的实现
(1)Proxy直接继承于被代理类,重新实现所有方法,在实现中调用被代理类的方法,相当于静态代理。客户端应用时,直接创建Proxy的实例。如果使用Autumn DI框架,则对Proxy生成Wrapper,进行注入。这种方式的好处是比较简单,可以单独使用而跟Autumn DI框架没有关系结,如果跟Autumn DI框架结合,只要创建Proxy的Wrapper即可。坏处是对于工厂模式(包括工厂方法和抽象工厂),不对原代码做修改的话,就不能创建Proxy的实例。关系如第一个图。
又有人关注Autumn了
关于Autumn Framework的多重继承问题
1、 1、问题描述
Autumn是实现C++依赖注入的一个框架。C++是可以多重继承的,Autumn在处理一个多重继承的注入时存在如下问题。
假设一个类的定义如下:
在把A的实例当作A1进行注入时,可能没有问题;但当作A2进行注入时,并不能将A的指针正确的转换为指向A2的指针。对同一个实例,A的指针和A2的指针会有一定的偏移。
2、 2、解决思路
-
a) 运行时动态判断并转换
如果Autumn能在运行时动态判断将要转换成哪个父类的指针,并做正确的处理,是最好的办法。但是C++不支持运行时的动态判断及转换。即使是dynamic_cast也是需要在编译时知道要置换的类型。不能够做到在运行时只提供一个父类的名称,就可以进行正确的转换。
-
b) 转换在编译时就处理好
即,在编译Wrapper时就知道该类共有哪些父类,于是也就确定了可能会有哪些转换,把这些转换都处理好。在运行时,只要向Wrapper提供父类的名称,就可以选择正确的转换进行处理。这种方式算是比较可行的。
但,问题是,如何知道一个类共有哪些父类呢?按照现在自动生成Wrapper的方式(AutumnGen),很容易知道直接继承的父类有哪些,但间接继承的父类有哪些就很难知道了。而间接继承的父类很可能就是作为注入类型的类。现在考虑有两种方式来知道有哪些父类:
-
i. (1)修改AutumnGen,自动找出一个类的所有父类,包括直接和间接继承的。但实现起来应该比较麻烦,而且也不好判断上溯到多少辈比较合适。
-
ii. (2)在使用AutumnGen时,让开发人员指出一个类的父类有哪些,只需要指出可能作为注入类型的父类即可。但让开发人员做这些事情感觉不是很好:他还要考虑用这个类的人想把它作为什么类型注入。从另方面想,也还可以接受:开发人员开发的这个类,可能作为哪些类型进行注入,开发人员应该清楚。还可以修改一个Autumn,对于直接继承的父类,不需要开发人员再次来指定。
依赖倒置、控制反转和依赖注入的区分
- 依赖倒置原则
- 控制反转
- 依赖注入
今天编程所犯的两个错误
auto_ptr<void_ptr> pp( new void_ptr[num] );
pv = pp.get();
// add argument into pw, not pwc
pv[i] = (*pargs)[i]->takeoutValue(pw.get(), this->ManagerOfType);
}
}
p = pwc->createBean(bc->getConMethodName(), pv, num);
虚析构函数与多重继承
public:
~baseA(){
cout<<"This is in baseA::~baseA()"<<endl;
}
};
public:
virtual ~baseV(){
cout<<"This is in baseV::~baseV()"<<endl;
}
};
public:
~derivedV(){
cout<<"This is in derivedV::~derivedV()"<<endl;
}
};
{
baseV *p = new derivedV();
baseA *p1 = new derivedV();
delete p;
delete p1;
}
This is in derivedV::~derivedV()
This is in baseV::~baseV()
This is in baseA::~baseA()
Delete baseA:
This is in baseA::~baseA()
虚函数与析构函数
public:
virtual void f1(){
cout<<"This is in baseV::f1()"<<endl;
}
virtual ~baseV(){
f1();
cout<<"This is in baseV::~baseV()"<<endl;
}
};
public:
virtual void f1(){
cout<<"This is in derivedV::f1()"<<endl;
}
~derivedV(){
f1();
cout<<"This is in derivedV::~baseV()"<<endl;
}
};
{
baseV *p = new derivedV();
delete p;
}
运行VC8的程序,误用VC6的动态库!
Autumn跟Tuscany的实现思路有点儿类似
Solaris上的make略有不同
depend::
$(CXX) -I../include -MM *.cpp >$(DEPENDFILE)
@echo "OBJS= //" >$(OBJSFILE)
@ls *.cpp | sed -e 's/cpp$$/o ///' >>$(OBJSFILE)
适当的日志很有用
模式-静态工厂方法
日志的级别
AIX下动态库的释放
奇怪的代码
#include <string>
#include <map>
using namespace std;
{
public:
void f1(){
printf("aaaaaa/n");
}
};
{
map<string, A*> *m1, m3;
map<string, void*> m2;
m2.insert(make_pair(string("a"), (void*)pa));
m1 = (map<string, A*>*)(&m2);
A* pa2 = m1->find("a")->second;
pa2->f1();
return 0;
}
函数参数的压栈顺序
class A
{
public:
A(int i, int j, int k){
printf("k=%d:[%x]/n",k,&k);
printf("j=%d:[%x]/n",j,&j);
printf("i=%d:[%x]/n",i,&i);
}
};
int main()
{
static int i=0;
f(i++,i++,i++);
A af(i++,i++,i++);
return 0;
}
{
int l;
int g;
printf("k=%d:[%x]/n",k,&k);
printf("j=%d:[%x]/n",j,&j);
printf("i=%d:[%x]/n",i,&i);
printf("___________/n");
printf("l:%x/n",&l);
printf("g:%x/n",&g);
return 0;
}
既开始,则坚持
AutumnFramework 0.1.0 beta for Windows VC6 released
This project implements Dependency Injection for C++. It supports property setter injection and constructor injection. First it is compiled with VC6 and will be compiled on multiple platform. It supports POCO (Plain Old C++ Object) and has no constraints on the classes you create. It may has half-incursion to your program because you should define bean's wrapper using some macros.
2007-01-16 Ver 0.1.0 beta for Windows VC6 released. You can download it here.
It may be very babyish now, because I don't know Spring well and do this to imitate Spring in a hurry. I will write some doc ASAP. Now, it support:
- Support property injection and constructor injection.Only two constructor now, one has arguments and one has no argument. The setter function name should be like setXXX where XXX is a property name. You may not obey that rule if you rewrite the file BeanWrapperMacro.h.
- Support following basic type: char, short, int, long, float, double, char*, string, and some docrated with unsigned.
- Support class injection. (A class is named as a bean in Autumn like in Spring.)
- Support customized type using interface IBasicType.
- Support pointer to above types.
- Support singleton.
- Support initialization and destroyation functions.
- Support property setter overloading.
- Supoort dependence(it may be not like Spring).
- Support multiple dynamic libraries and local library(bean is in main process).
- Support configuration of log file path and level.