如何将类别与不同的实现分开

How to separate a class from its different implementations

本文关键字:实现      更新时间:2023-10-16

我有一个具有显式类模板专业化和另一个部分类模板的类模板。

template< typename T1, typename T2 >
class A{
    internal_representation1 inRep;
    // methods
};
template<>
class A < specific_type_1,specific_type_2 >{
     //internal represenation different for this type.
    internal_representation2 inRep;
    // methods
};
template< template T1 >
class A < T1,specific_type_2 >{
     //internal represenation different for this type.
    internal_representation3 inRep;
    // methods
};

专业化导致接口重复。类模板及其专业都具有相同的方法名称,但其实现都不同。特别是其内部数据结构的表示。

我应该用桥设计模式替换上述实现吗?

注意:这个问题与我们如何使用boost :: mpl?

实现构建器设计模式有关

您还可以使用状态模式(适用于不同的内部表示形式),但这取决于您项目中的需求。状态模式允许您使用相同的对象,但是如果您需要不同的对象,也可以使用工厂方法。