Re: Is the Mixin pattern accepted in all camps?
DeMarcus wrote:
Hi,
As I understand, Policies have been widely accepted, but is it the same
with Mixins? Or does people still believe Mixin is a bad way to avoid
Liskov's IS-A principle?
My latest Mixin looks like this.
template<class T>
class CopyMixin
{
public:
typedef std::shared_ptr<T> SPtr;
SPtr copy() const
{
// Use NVI.
return copy_();
}
protected:
virtual SPtr copy_() const = 0;
};
class SomeClass : public CopyMixin<SomeClass>
{
private:
virtual SPtr copy_() const
{
SPtr c = /* Make a proper deep copy. */
return c;
}
};
What do you think about Mixins in general?
And if you have time, what do you think about above CopyMixin?
(Maybe a better copy mixin is already invented. If you know of any,
please show me.)
I'm afraid you're confusing mix-ins with CRTP (curiously recurring
template pattern). Your CopyMixin is not a mix-in class, it's a base
template parametrized on the derived class, a classical CRTP.
Mix-ins are classes that inherit from a template parameter.
I.e.
template <typename Base>
class Mixin : public Base
{
};
My personal opinion is that mix-ins are a great tool for certain
circumstances. I don't think that violate Liskov's principle, rather
they "templatize" the subclass. I.e., every concrete Mixin IS-A a
specific Base.
BTW, a couple of years ago Andrei Alexandrescu wrote a paper with Emery
Berger that described a policy-based memory allocation technique that
heavily relied on mix-ins.
[excess quoting deleted--mod]
Andy.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]