Re: design problem with inheritance
On Aug 28, 6:18 pm, Noah Roberts <roberts.n...@gmail.com> wrote:
On Aug 27, 4:24 pm, James Kanze <james.ka...@gmail.com> wrote:
On Aug 27, 9:14 pm, Noah Roberts <roberts.n...@gmail.com> wrote:
[...]
Actually, I thought of a better example. I see the strategy pattern
as a way to replace behavior dynamically. There is a static version
of the strategy pattern in C++ using templates, but as far as "design
patterns" goes I see this as primarily a runtime alteration.
There are generally three alternatives: templates with a traits
class (compile time decision), the template method pattern
(runtime decision at construction time, then fixed) and the
strategy pattern (allows changing the decision on an already
constructed object).
When I was mentioning a "static" version of the strategy pattern I was
specifically referring to "policy based design" where one cobbles
together a larger class out of "policy" classes that are basically
strategies within some concept.
This is more or less what I understood. Typically, the "policy"
class(es) will be traits, but you're correct that they don't
have to be; there are many ways to customize behavior, and
traits are just the most common (at least in the circles I
frequent).
[...]
This is closer to the strategy pattern than traits I think since
traits generally provide methods to retrieve information about a type
and rarely provide behavior alternatives themselves.
Traits can provide behavior. The usual definition for a traits
class is that it is a class with no non-static functions or data
members. (I believe that this is the definition used by the
originator.) But it's really just a question of vocabulary.
Strictly
speaking though, I'm glad he called it something else.
The word "policy" is more descriptive of the use; "traits", as
defined above, is more a description of "how" (and a rather
arbitrary one at that).
Note that the idea itself is hardly new; I used it with
<generic.h>, before we had templates, and I'm sure that I didn't
invent it. Giving the pattern a name is, however, as is
formalizing it.
--
James Kanze