Re: Policy-based class design or inheritance?
On 29 Set, 21:04, puzzlecracker <ironsel2...@gmail.com> wrote:
On Sep 29, 12:29 pm, Vincent <vdu...@gmail.com> wrote:
Hi all,
suppose we can choose between two approaches:
1) First approach (policy-based class design)
[CUT]
2) Second approach (classic inheritance)
[CUT]
Instinctively I tend toward the first approach. Inheritance is
undoubtedly a "pillar" of object-oriented programming, but if possible
I always look for an alternative design. (Inheritance introduces
issues not always obvious: it's really an "is a" model? the base class
should be made abstract? the polymorphic use of the class should be
allowed? the inheritance should be of interface or of implementation?
and so on.)
What alternative do you prefer?
Vincent
It seems that both approaches need to include A.h file, hence original
coupling persist. I would use stratagy and passing a pointer (evia
ctor or setter) to AToStderr class
class OutputToStderr {
static void setPrinter(Printer *printer) {
_printer=printer;
}
print();
private:
static Printer _printer
};
True, this is another solution, but in my specific case I don't need
the strategy pattern, because its
peculiarity is to select an algorithm "at runtime", while I want
select it "at compile time" (hence the idea of use a policy).
The fact that through the strategy pattern I can remove the #include
"A.h" in my case does not involve major benefits.
Vincent.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]