【发布时间】:2017-12-14 03:40:57
【问题描述】:
根据wiki,
依赖项是可以使用(作为服务)的对象。
Image processing 应用程序是面向 4 个角色的 OOP 范式样式 C 语法,如下所示。
1) 接口 (handlers.h)
typedef struct {
int (*canHandle) (char *);
int (*drawImage)(char *);
int (*savefile)(char *);
}imageHandler;
2) 取一个依赖 (gifhandler.c)
imageHandler gifhandler = {
gif_canHandle,
gif_drawImage,
gif_savefile
};
3) 依赖容器(由config.c处理)
//gifhandler.c - dependency
int _init(){
printf(" registering gifhandler \n");
reg_handler(&gifhandler);
return 0;
}
//config.c
imageHandler *imagehandlers[10];
int reg_handler(imageHandler *ih){
// we need to perform checks here.
imagehandlers[libs] = ih;
libs++;
return TRUE;
}
// config.c
int init_handlers(){
.....
soptr = dlopen(so_name,RTLD_NOW);
....
}
4) 客户端 - 服务定位器 (UI.C)
// UI.C
switch(choice){
case 1:
vdrawImage(filename);
break;
case 2:
vsavefile(filename);
break;
}
// viml.c
int vdrawImage(char *filename){
...
handleno = find_handler(filename);
...
ih=imagehandlers[handleno];
ih->drawImage(filename);
return FALSE;
}
// viml.c
int vsavefile(char *newfilename ){
...
handleno = find_handler(newfilename);
...
ih=imagehandlers[handleno];
ih->savefile(newfilename);
}
1)要在Dependency容器中添加新的依赖项(libxyzhandl.so.1),只需要在config.txt中添加一个新条目configurable,如下图,
config.txt
./libgifhandl.so.1
./libtiffhandl.so.1
2) ./libxyzhandl.so.1 提供的新服务,将包含在依赖容器中无需重新编译应用程序。
3) 完整应用程序的测试不需要,libxyzhandl.so 的源代码除外。
因此,如果 config.txt 为空,则应用程序什么也不做,除了对任何输入(图像文件)说 We cannot handle this kind of files,显示为 here。
下面是调用流程的可视化,
问题:
1) 依赖容器与IOC容器有区别吗?
2) Spring IOC 容器 是否提供了比仅仅维护依赖项更多的功能?
【问题讨论】:
标签: javascript c spring design-patterns ioc-container