Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Independently partial interface implementation

232 views
Skip to first unread message

Zakharov Sergey

unread,
Apr 2, 2013, 7:35:43 PM4/2/13
to
{ 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! ]

DeMarcus

unread,
Apr 7, 2013, 4:45:58 PM4/7/13
to
Hi Sergey,

Your pattern is very similar to the Decorator pattern and a C++
variant of it that normally goes under the name Mixin.
http://en.wikipedia.org/wiki/Decorator_pattern
http://en.wikipedia.org/wiki/Mixin

The base of your idea has been around for a while. See
http://www.drdobbs.com/cpp/mixin-based-programming-in-c/184404445

However, I think it's worthwhile discussing this way of combining
implementations. It touches the field of Aspect-Oriented Programming
that I believe is something powerful.
http://en.wikipedia.org/wiki/Aspect-oriented_programming

Other patterns related to your work that I highly recommend you to
take a look at are:
CRTP - http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
Policy pattern - http://en.wikipedia.org/wiki/Policy-based_design
NVI - http://en.wikipedia.org/wiki/Non-virtual_interface_pattern

A really good book working a lot with generics and polymorphism is
Modern C++ Design, by Andrei Alexandrescu
http://www.amazon.com/dp/0201704315

As for the feedback to you I can only say that I like this way of
programming that you do. However, I've never put it into production
code so I haven't seen the bad sides of it yet. Hopefully someone else
can provide you with that kind of experience.

Best regards,
Daniel

PS. Never forget the virtual destructor in your interfaces, not even
in examples. ;)

Stuart

unread,
Apr 9, 2013, 1:26:22 PM4/9/13
to
On 04/07/2013, DeMarcus wrote:
> PS. Never forget the virtual destructor in your interfaces, not even
> in examples. ;)

Chances ar high that OP uses his mix-in classes for MS COM
programming. In that case the interfaces must not have virtual
destructors.

Regards,
Stuart

Wil Evers

unread,
Apr 9, 2013, 1:56:52 PM4/9/13
to
Stuart wrote:

> On 04/07/2013, DeMarcus wrote:
>> PS. Never forget the virtual destructor in your interfaces, not even
>> in examples. ;)
>
> Chances ar high that OP uses his mix-in classes for MS COM
> programming. In that case the interfaces must not have virtual
> destructors.

Ouch! They really messed things up, didn't they?

- Wil

DeMarcus

unread,
May 4, 2013, 8:33:26 AM5/4/13
to
> 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.
>

[...]

>
> 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.
>

I came to think of that I was doing something similar to you not so long
ago. Maybe the following could be something that you can take ideas from.

https://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/4939f2da36e7fb88

Best regards,
Daniel
0 new messages