Re: What is the name of this pattern?
mike.reed10@googlemail.com (Mike Reed) wrote (abridged):
However, External Polymorphism is about taking classes that
have already been written and making it look like they inherit from
a base class with virtual functions of your choice, without having
to make any changes to those classes.
The "Applicability" section is a bit more general. It says:
1. Your class libraries contain concrete data types that
cannot inherit from a common base class containing
virtual methods; and
Personally I'd stick the word "dynamic" in there a few times, but
basically I think that matches your situation: it's not about whether the
client classes are "already written". It's enough that you don't want
them to share a common base class or have virtual functions.
The motivation for my example is very different. I'm writing my
class to provide some functionality that other people can later
use without my class being type intrusive, although it still
requires users classes to provide specific methods, much akin to
duck typing.
To me that looks pretty much the same.
Normally this would just be a templated class. The motivation for
the technique I have shown is to avoid templating the *whole* class
so that as much of the implementation as possible can be hidden.
That's why I'd add the word "dynamic". Admittedly when most people say
"polymorphism" they mean "dynamic polymorphism", but in C++ we also have
static polymorphism through templates. Using dynamic polymorphism for
information hiding is pretty standard.
I have also seen code like this called "Adaptor". In fact, I think the
original Design Patterns book has almost identical code under that name.
I agree "Adaptor" would be a poor name for what you are doing because it
doesn't reflect your intent. You aren't adapting one interface to another.
You are making the same interface available through dynamic polymorphism,
and doing it externally, without modifying the class to add virtual
functions.
I think you are hung up on the fact that we normally think of dynamic
polymorphism as being used to hide the implementor of the virtual
function from the caller of it. In this case we are doing the opposite:
hiding the process() function from clients. However, this is still a
standard application of dynamic polymorphism. Polymorphism has always
been as much about reusing as being reused.
If there is a problem with the name, "External Polymorphism", is that it
doesn't say what the polymorphism is for. I can understand wanting a more
specific name. However, in my opinion it at least doesn't imply a use
different to what you are doing.
-- Dave Harris, Nottingham, UK.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]