Mixins
Java Layers Home


Layer composition in Java Layers is based on the use of mixins.  Mixins are types whose supertypes are parameterized. Mixins are not supported in standard Java, but are available in some languages that support parameterized polymorphism such as C++.


Mixins are useful because they allow a set of classes to be specialized in the same manner, with the specializing code residing in a single class definition.  For example, suppose we wish to extend three unrelated classes--Car, Chest and House--to be 'lockable' by adding two methods, lock() and unlock().  Without mixins, we would define subclasses of Car, Chest and House that each extended their respective superclasses with the lock() and unlock() methods.  The lock code would be replicated in three places.  With mixins, we would instead write a single class called Lockable that could extend any superclass, and we would instantiate the Lockable class to extend Car, Chest and House.  The lock() and unlock() methods would only be defined once.  In JL syntax, the Lockable mixin would be defined as follows: 

class Lockable<T> extends T {
 public lock(){..}
 public unlock(){..}
}