Abstract Mixin

An Abstract Mixin is an implementation of the MixinType interface, but is an abstract class and has not implemented all the methods. The Qi4j runtime can use multiple Mixins for each MixinType interface. It is also possible to let Generic Mixins handle the remaining missing methods.

Let's look at an example;

public interface Car
{
    void startEngine();

    void stopEngine();

    Property<Float> speed();

    void accelerate( int acceleratorPosition );

    void break( int breakForce );
}

public abstract class SpeedMixin
    implements Car
{
    private long lastTime;
    private float currentSpeed;

    public SpeedMixin()
    {
        currentSpeed = new ComputedPropertyInstance<Float>()
        {
            public Float get()
            {
                return currentSpeed;
            }
        };
    }

    public Property<Float> speed()
    {
        // Update the speed from time past and
        // accel/break applied.
        return currentSpeed;
    }

    public void accelerate( int acceleratorPosition )
    {
        // Update the speed from time past and
        // accel/break applied.
    }

    public void break( int breakForce )
    {
        // Update the speed from time past and
        // accel/break applied.
    }
}

In the example above, the SpeedMixin only implements some of the methods in the Car interface. By declaring the SpeedMixin abstract, the compiler will have no problems with it, and the Qi4j runtime will create the necessary subclass with the additional methods. But since the Mixin will only be serving the methods that it supports, the generated subclass's startEngine() and stopEngine() methods, will never be called. They must be provided by some other Mixin.

Qi4j and the Qi4j logo are trademarks of Richard Öberg, Niclas Hedhman and the members of the Qi4j Core Team. See Qi4j licensing for more information.
Powered by SiteVisionexternal link.