Independently partial interface implementation
{ Please limit your text to fit within 80 columns, preferably around 70,
so that readers don't have to scroll horizontally to read each line.
This article has been reformatted manually by the moderator. -mod }
Many people believe that mixing Generic Programming with Polymorphism
is a bad idea, because these concepts are very different, orthogonal.
Many programmers are quite sure these two just do not work together.
However, I think I know at least one case when Generic Programming
extends Polymorphism.
I have created a new pattern allows to implement not full interface,
but by parts. Then, combining the partial implementation you can
easily create specific types you need.
Example:
class AB {
public:
virtual int a() = 0;
virtual const char* b() = 0;
}:
template <class TBase> class ImplA1 : public TBase {
public:
virtual int a() {
return 1;
}
};
template <class TBase> class ImplA2 : public TBase {
public:
virtual int a() {
return 2;
}
};
template <class TBase> class ImplB1 : public TBase {
public:
virtual const char* b() {
return "10";
}
};
template <class TBase> class ImplB2 : public TBase {
public:
virtual const char* b() {
return "20";
}
};
Now we can construct new types combining partial implementations of
AB interface:
typedef ImplA1<ImplB1<AB>> A1B1;
typedef ImplA2<ImplB1<AB>> A2B1;
typedef ImplA1<ImplB2<AB>> A1B2;
typedef ImplA2<ImplB2<AB>> A2B2;
Any feedback very appreciated.
Thanks,
Sergey
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]