Some help on mixins

Looking up the definition of a mixin I’ve come up with this.
Is this what you guys would call a mixin?


public class EntitySorter
{
       private Mixin mixin;


       public void sortGlobalCards(Card[] globalCards) {
		mixin.loop(globalCards, true);
	}

	public void sortProcessedCards(Card[] toProcess) {
		mixin.loop(toProcess, false);
	}
	
	protected static void sortGlobalCards(Card globalCard) {
	}

	protected static void sortProcessedCards(Card toProcess) {
	}
	
	
	private static final class Mixin
	{
		public void loop(Card[] cards, boolean isGlobalSet) {
			for (int i = 0; i < cards.length; i++) {
				if(isGlobalSet)
					sortGlobalCards(cards[i]);
				else
					sortProcessedCards(cards[i]);
			}
		}
	}
	
}

Mixin seems to have a couple of different meanings. The most common one I’ve found is that of an ‘abstract subclass’:

[quote]Mixins are classes parameterized by their parent class
[/quote]
which corresponds to

class Mixin<T> extends T {
}

in Java. Unfortunately, this is not possible with java generics. Bruce Eckel wrote a couple of posts about this some time ago.
I found a pretty verbose approximation to mixins, which is also the only way I know of in java to get this kind of behaviour.

You could do the same with


class MyMixin<T> {
  private T t;
  public MyMixin(T t) {
    this.t = t;
  }
  public T get() {
    return(t);
  }
}

and then simply call

get().doSomething()

instead of

doSomething()

Have I forgotten something? ::slight_smile:

Well that’s not really a mixin anymore based on the definition from the paper Bruce Eckel links to.

[quote]Nearly 20 years ago, the Lisp object-oriented community invented the term mixin to describe a class with a parametric parent such as the TimeStamped class above. The name was inspired by the fact that such classes can be mixed together (via subclassing) in various ways, like nuts and cookie crumbs in ice cream.

class TimeStamped extends T {
public long time;
TimeStamped() {
super();
time = new java.util.Date().getTime();
}
}
[/quote]

I’ll try to actually answer your question this time ;D
To be honest, I’m not quite sure what you are trying to accomplish with the code example above. Only considering the code you posted it seems kind of pointless, since the Mixin class is completely internal to EntitySorter. Mixin#loop could just as well be a method of EntitySorter.
I get the impression you want to externalize the ‘loop’ behaviour from the EntitySorter class. If that’s the case, that’s called the strategy pattern.

Oh.
I really don’t know the names of all these patterns.
It’s funny how I come up with them though.
Thanks guys.

From what I know a mixin (a mix in actually) is when you use multiple inheritance of implementation to add some small fully implemented functionality to class. To simulate this in Java you use a combination of an interface for the inheritance and a delegated class for the implementation, like


interface Mixin {
    void m1();
    void m2();
}
class MixinImpl implements Mixin {
   void m1() {
   }
   void m2() {
   }
}
// usage
class C extends Whatever implements Mixin {
   MixinImpl m = new MixinImpl();
   void m1() {
      m.m1();
   }
   void m2() {
      m.m2();
   }
}

As a fact this simulation of multiple inheritance of implementation is more flexible that the static one offered by for example C++ because you can easily replace the implementation during runtime if you want.