2

I have a class template with an explicit class template specialisation and another partial class template specialisation.

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

The specialisations leads to interface duplication. The class template and its specialisations all have the same method names but differ in their implementations.Specifically, the representation of their internal data structure.

Should I replace the above implementation with the bridge design pattern ?

note: This question is related to How can we implement the Builder design pattern using boost::mpl?

Community
  • 1
  • 1
Samrat Roy
  • 105
  • 8
  • Are internal_representation's totally differs? – Denis Ermolin Oct 25 '12 at 08:05
  • Yes, one is `std::vector`, the second is an `boost::dynamic_bitset` and they third is an `ComplexUDT`. These types are the ones supported currently, in the future the code may need to support more. Hence it is important to separate the interface from the implementation. – Samrat Roy Oct 25 '12 at 09:26
  • 2
    You have a **template** with two explicit instantiations. A template is **not** a class. – Pete Becker Oct 25 '12 at 14:32
  • @PeteBecker Thank you for your response. I was under the misconception that a template specialisation is a separate instance of the same class. Could you please elaborate on your statement or point me towards related texts ? Thank you. – Samrat Roy Oct 25 '12 at 15:02
  • 2
    I was objecting to "I have a **class** with three different implementations." Even though it's called `class A`, `A` is a class template. That is, it's a pattern for creating classes. So `A` is a class. `A` is also a class, but more formally it's an explicit specialization of the template `A`. And `A` is a partial specialization of the template `A`; partial because it doesn't provide all of the template arguments. – Pete Becker Oct 25 '12 at 15:23
  • @PeteBecker Thank you! I have rephrased my post with respect to your comments. – Samrat Roy Oct 26 '12 at 07:19

1 Answers1

0

You can also use the state pattern (good for different internal representations), but it depends on what you need in your project. The state pattern allows you to use the same object, but if you need different objects, you also can use the factory method.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
Tomasz Dzięcielewski
  • 3,829
  • 4
  • 33
  • 47
  • In the current implementation template specialisation handles the problem of choosing the correct class. But this leads to duplicating the interface definitions in three classes. Any changes to the "abstract" interface would require the changes to be propagated to all three implementations. Which is a maintenance nightmare. Maybe, CRTP would yield a better design ? – Samrat Roy Oct 25 '12 at 09:36