2012-10-25 69 views
2

我有一個具有顯式類模板特化和另一個部分類模板特化的類模板。如何從不同的實現中分離一個類

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 
}; 

專業化會導致界面重複。類模板及其特化都具有相同的方法名稱,但它們的實現有所不同。具體而言,它們的內部數據結構的表示方式不同。

我應該用橋接設計模式替換上述實現嗎?

注:這個問題是有關How can we implement the Builder design pattern using boost::mpl?

+0

internal_representation的完全不同嗎? –

+0

是的,一個是'std :: vector',第二個是'boost :: dynamic_bitset',第三個是'ComplexUDT'。這些類型是目前支持的類型,將來代碼可能需要支持更多。因此,將接口與實施分開很重要。 –

+2

你有一個**模板**和兩個顯式實例。模板是**不是**類。 –

回答

0

您還可以使用state pattern(適合不同的內部表示),但它取決於你在你的項目所需要的。狀態模式允許您使用相同的對象,但是如果您需要不同的對象,則還可以使用factory method

+0

在當前的實現模板中,專門化處理選擇正確類的問題。但是這會導致在三個類中複製接口定義。對「抽象」接口的任何更改都需要將更改傳播到所有三個實現。這是一場維護噩夢。也許,CRTP會產生更好的設計? –

相關問題