Re: Is the Mixin pattern accepted in all camps?
DeMarcus wrote:
As I understand, Policies have been widely accepted, but is it the same
with Mixins?
Policies or traits are harder to understand than mixins, IMHO. Other
than that, yes, mixins are a good way to separate a single feature out
into a separate class that can be debugged, reused and understood on its
own.
Or does people still believe Mixin is a bad way to avoid
Liskov's IS-A principle?
I wouldn't understand a mixin as a baseclass, as its its own
functionality is usually limited. To me, it's rather a feature that is
attached to a class and which is written as a baseclass in C++. In
Python you might use a decorator for that instead, e.g. there is one in
Python that can construct all comparison operators for a class from just
the less-than comparison.
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;
}
};
[...]
And if you have time, what do you think about above CopyMixin?
class base
{
// Note: never returns null
virtual base* do_clone() const = 0; // throw(std::exception)
public:
std::auto_ptr<base> clone() const // throw(std::exception)
{
base* p = do_clone();
// must not be null
assert(p);
// must have the same dynamic type
assert(typeid(*p) == typeid(*this));
return std::auto_ptr<base>(p);
}
};
class derived: public base
{
virtual derived* do_clone() const
{ return new derived(*this); }
public:
std::auto_ptr<base> clone() const // throw(std::exception)
{
base* p = do_clone();
// must not be null
assert(p);
// must have the same dynamic type
assert(typeid(*p) == typeid(*this));
return std::auto_ptr<base>(p);
}
}
Notes:
- I did not factor this out into a mixin but just showed the "normal"
implementation for easier reading.
- I now see who I'm sharing the object with: nobody. I have exclusive
ownership due to auto_ptr.
- Check derived class' implementation, both the pointer value and what
it points to as far as possible.
- Use covariant return types so that you can get the right type if you
know that you have a "derived" instance.
If you are happy without the covariant return types, you can just go and
transform "base" into a mixin and implement do_clone() manually for all
derived classes.
Otherwise, you might need two mixins, one being the equivalent to "base"
and the other to "derived". However, then you get a few complications
because one base's pure virtual functions can't be implemented by
inheriting another, you have to get the second baseclass in between your
real base class instead. Something like this:
template<typename T>
class abstract_clonable {...};
template<typename T, typename B>
class clonable: public B {...};
class base: public abstract_clonable<base> {...};
class derived: public clonable<derived, base> {...};
In other words, you have the following order in which classes inherit
from each other:
abstract_clonable<base> - declare clonable interface for base
base - user-defined content
clonable<derived, base> - implement clonable for derived
derived - user-defined content
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]