Mixin和接口实现
Mixin and interface implementation
根据http://www.thinkbottomup.com.au/site/blog/C%20%20_Mixins_-_Reuse_through_inheritance_is_good
但请稍等,这些都无助于我们完成任务Manager框架,因为类不实现ITask接口。这就是最后一个Mixin的帮助所在——Mixin引入了ITask接口进入继承层次结构,充当适配器在某种类型的T和ITask接口之间:
template< class T > class TaskAdapter : public ITask, public T { public: virtual void Execute() { T::Execute(); } virtual std::string GetName() { return T::GetName(); } };
使用TaskAdapter很简单——它只是链中的另一个环节混合蛋白。
// typedef for our final class, inlcuding the TaskAdapter<> mixin typedef public TaskAdapter< LoggingTask< TimingTask< MyTask > > > task; // instance of our task - note that we are not forced into any heap allocations! task t; // implicit conversion to ITask* thanks to the TaskAdapter<> ITask* it = &t; it->Execute();
当ITask
由MyTask
实现时,为什么需要TaskAdapter
?此外,如果ITask
不抽象,可能会导致钻石问题。
这是一篇非常有趣的文章。
在最后的Mixin示例中,MyTask
类是从ITask
派生的而不是。这意味着它不能被强制转换为ITask
指针,这是在最后完成的。
在该示例中,我相信您可以从ITask
导出MyTask
。但我认为作者想说明,您甚至可以通过使用TaskAdapter
来解耦MyTask
类。
相关文章:
- 如何从实现接口的模板化类实例访问结构
- 如何在C++中实现接口
- unique_ptr实现接口时对已删除函数的引用
- 避免对实现接口的类使用多个几乎相同的声明
- C#COM组装实现C 接口
- 当传递对类的引用时,优于另一个函数,即实现接口
- 通过其方法的子集实现接口
- C ++类实现接口,接口具有采用任何实现该接口的类的方法
- 错误:在 C++ 中实现接口模式时未定义对 typeinfo 的引用
- 与模板实现接口的类模板
- 在主应用程序中声明的 dll 中实现接口 - C++
- Objective-C类实现C 接口
- 如何添加实现C 接口的标签
- 强制接收实现接口的超类指针作为方法的参数
- 我可以部分实现接口层次结构吗
- 是否可以通过继承实现接口
- 在c++中实现接口
- 如何使用谷歌测试测试实现接口的不同类/结构
- 使用成员函数模板实现接口的功能
- C++ 11 实现接口的方法不可用。为什么?