C++极简的插件化抽象工厂(PAF) | 知行一


PAF a C++ pluginable abstract factory library



主要用在面向接口编程, 解耦接口实现者和使用者.

对于实现方: 主要提供非侵入式的注册组件方式,以及完全隔离组件功能,支持跨源文件跨模块重构.

对于使用方: 提供了非常便捷的使用方式.
0. 类型安全.仍然能在构造中使用参数,仍然能享受编译期类型安全检查.
1. 扁平化.无依赖只要包含接口头文件,即可使用.无需额外依赖.
2. 额外提供更好用的单例和更强大的全局共享对象.
3. 支持优雅退出检查,循环引用检查.
4. C ABI 友好.
5. 可扩展IOC支持.


I have a interface and it‘s implemented as below, with a method bar:

with PAF, we need only three line noninvasive code

wherever needed (cross-source/cross-module), we just need to include the interface file for foo_i. (consistent with the interface usage scenario, no additional actions are required).

this is magic show time, which creates local objects with parameter.

the whole process can also rely on the compiler to check for errors. objects created through factories are naturally smart pointers.

of course, it also supports the creation of singleton objects:

singleton will be destroyed manually or by factory with same order as statics. it is more flexible and controllable than the general static singleton.

in addition, a more secure pattern is implemented than a single case control: global shared objects. usually as parent of sub objects. to break circle reference and parameter passing to subs.

in particular, global shared objects can be shared across modules, based on the base layer of the abstract factory. shared objects are destroyed only when all module references are destroyed.

based on the above features, you can also support elegant, leak-free exits. Perform leak and circular reference checks during release.