unmesh
Posts: 15
Nickname: unmesh
Registered: May, 2003
|
|
Re: Mixins: Something Else You Can't Do With Java Generics?
|
Posted: Oct 24, 2005 5:34 PM
|
|
Its not possible to extend existing class as template parameter. But you can simulate inheritance explicitly. Here is the example from the same site.
/** * Application-independent support for base classes and mixin classes. * Conceptually, an instance of this class has a superclass instance * (whose methods are statically bound) and a current receiver * (whose methods are dynamically bound). */
class Composable {
/** * The static superclass instance for this object, that is, the next * object up the conceptual inheritance hierarchy. */
protected Composable zuper;
/** * The dynamic receiver instance for this object, that is, the object * at the bottom of the conceptual inheritance hierarchy (beginning * of the chain of responsibility). */
protected Composable thiz;
/** * This methods sets the superclass instance for this object. */
private void setSuper(Composable zuper) { this.zuper = zuper; System.out.println(this + ".zuper = " + zuper); }
/** * This method recursively sets the current receiver for this object * and its conceptual superclass instances. */
private void setThis(Composable thiz) { this.thiz = thiz; System.out.println(this + ".thiz = " + thiz); if (zuper != null) { zuper.setThis(thiz); } }
protected Composable(Composable zuper) { setSuper(zuper); setThis(this); }
protected Composable() { this(null); } }
/** * A basic collection. */
interface Collection { void insert(Object item); Object remove(); void clear(); boolean isEmpty(); }
/** * Application-specific support for base classes. Subclasses should * implement all methods in the application-specific interface, that is, * Collection. For convenience, it provides application-specific * accessor methods for thiz and zuper. */
abstract class CollectionBase extends Composable implements Collection { protected CollectionBase() { } protected CollectionBase(Composable zuper) { super(zuper); } protected Collection getThis() { return (Collection) thiz; } protected Collection getSuper() { return (Collection) zuper; } }
/** * Application-specific support for mixin classes. Subclasses should only * implement those methods in the application-specific interface that they * want to refine. For convenience, it provides default implementations * of all methods in the application-specific interface; this simulates * method inheritance. */
abstract class CollectionMixin extends CollectionBase { protected CollectionMixin(Composable zuper) { super(zuper); } public void insert(Object item) { getSuper().insert(item); } public Object remove() { return getSuper().remove(); } public void clear() { getSuper().clear(); } public boolean isEmpty() { return getSuper().isEmpty(); } }
/** * An example implementation of Collection to be used as a base class. * Note that methods that the subclass possibly refines must be invoked * via getThis. */
class Queue extends CollectionBase { private LinkedList items = new LinkedList(); public void insert(Object item) { items.addLast(item); } public Object remove() { return items.removeFirst(); } public void clear() { while (! getThis().isEmpty()) { getThis().remove(); } } public boolean isEmpty() { return items.isEmpty(); } }
/** * An example implementation of Collection to be used as an * abstract subclass (mixin) for adding the SizeOf capability * to an arbitrary Collection implementation. Note that methods * from the superclass in the conceptual sense must be invoked * via getSuper. */
class SizeOf extends CollectionMixin { private int count; public SizeOf(CollectionBase zuper) { super(zuper); } public void insert(Object item) { getSuper().insert(item); count ++; } public Object remove() { Object result = getSuper().remove(); if (result != null) { count --; } return result; } public int size() { return count; } }
/** * An example implementation of Collection to be used as an * abstract subclass (mixin) for adding the Verbose capability * (reporting on every operation) to an arbitrary Collection * implementation. */
class Verbose extends CollectionMixin { public Verbose(CollectionBase zuper) { super(zuper); } public void insert(Object item) { getSuper().insert(item); System.out.println(this + ".insert(" + item + ")"); } public Object remove() { Object result = super.remove(); System.out.println(this + ".remove() -> " + result); return result; } public void clear() { super.clear(); System.out.println(this + ".clear()"); } public void reset() { System.out.println(this + ".reset()"); } }
/** * Main class for testing this mixin framework. */
public class Mixin { public static void main(String[] args) { SizeOf s = new SizeOf(new Verbose(new Verbose(new Queue()))); s.insert("hello"); s.insert("world"); System.out.println(s.size()); // prints 2 s.clear(); System.out.println(s.size()); // prints 0 // note that we cannot invoke reset on s // because SizeOf is not a subtype of Verbose } }
|
|