Mixin On Delegators

Consider that some mixins have been defined for developing GUI widgets.

These mixins are all about composing complicated kinds of objects at compile time.

What if we want to give end users the power to compose complex objects dynamically - i.e. to support run time assembly? Can we make use of all the mixins for this purpose? In other words can the mixins be used to help construct run time components that support run time assembly?

Run time assembly depends on an abstract interface supporting dynamic polymorphism. For example using an abstract base class


// Abstract Base Class uses virtual methods
// to allow for dynamic polymorphism.
struct GuiControl
{
    virtual int GetWidth() const = 0;
    virtual int GetHeight() const = 0;
    < etc >
};

For run time assembly the decorator design pattern [] is of interest. A decorator object could apply a rotation of 90 degrees to the GUI control that it decorates.

An interesting solution is to implement a run time decorator as a mixin on an interface delegator.

Consider that we have a general purpose delegator for the above polymorphic interface. The delegator implements the interface by passing on every method call to a delegate - it is basically an indirection.


// General purpose delegator forwards on
// method calls to its delegate
struct GuiControlDelegator : public GuiControl
{
    int GetWidth() const
    {
        return delegate->GetWidth();
    }
    int GetHeight() const
    {
        return delegate->GetHeight();
    }
    < etc >

    GuiControl* delegate;
};

Now it is easy to convert each mixin into a corresponding run time decorator. For example:


// Applying the mixin to the delegator
// gives a run time decorator
struct Rotate90Decorator : Rotate90<GuiControlDecorator>
{
};

Note that as a result of inlining the run time decorator is just as efficient as a version coded directly without mixins.