Alex Rivera | Logout

Implement Mixin In Java?

Asked 2009-02-25T19:29:22.560
60

Using Java 6, how can I implement a mixin? It is very easy and possible in Ruby. How can I get similar in Java?

Edit
Report

5 Answers

23

You could use CGLIB for that. The class Mixin is able to generate a dynamic class from several interfaces / object delegates:

static Mixin    create(java.lang.Class[] interfaces,
                        java.lang.Object[] delegates)
static Mixin    create(java.lang.Object[] delegates)
static Mixin    createBean(java.lang.Object[] beans) 
answered 2009-02-26T18:36:33.163
5

In the sense that a Ruby mix-in is the equivalent of a Java abstract class, no, you cannot implement a mix-in in Java. You can come close by using interfaces and thus defining absolutely no code in your mix-in, but you cannot directly achieve the same behavior as in a Ruby mix-in.

answered 2009-02-25T19:40:42.673
5

UPDATE: Qi4j is now Apache Polygene, https://polygene.apache.org

Qi4j's definition of Mixins are probably quite unique, as it doesn't start with a base class. By going to that extreme, a whole new paradigm of how applications are built up emerges, and we call that Composite Oriented Programming. The Composite is the 'object' equivalent and not only are Mixins wired together, but also Constraints (validation), Concerns (around advice) and SideEffects (can't alter the method outcome).

So I think Qi4j has a very strong Mixin story to tell. Mixins can be 'typed' or 'generic', they can be public (accessible outside the composite) or purely private (within the Composite). Qi4j strongly defines what Properties are, and goes on to have built-in persistence, which doesn't leak the storage implementation into your domain (caveat; Qi4j leaks to your domain). And once persisted entities enters the picture, strong definition of a Associations are also required (and included in Qi4j).

See http://www.qi4j.org/state-modeling.html for a good overview.

In Qi4j, ONLY Mixins have state. Constraints/Concerns/SideEffects can't have state (if they do they need to referens a private mixin).

To define a composite in Qi4j, it is possible to either do it structurally on the types themselves, OR at bootstrap time when the runtime model is created.

Structurally;

@Mixins({PetrolEngfineMixin.class, FourWheelsMixin.class})
public interface Car extends HasEngine, HasWheels, EntityComposite
{}

At boot time;

public interface Car
{}

public class CarModuleAssembler implements Assembler { public void assemble( ModuleAssembly module ) { module.entities( Car.class ) .withMixins( PetronEngineMixin.class, FourWheelsMi

answered 2012-11-03T11:41:19.057
2

You can do Mixins with Java now (ie 5,6,7) using AspectJ ITDs. Java 8 of course will add better capabilities with its defender methods.

answered 2013-02-01T16:07:44.027
2

Not sure exactly what features from mixins you're looking for but much of it can be done using the decorator pattern.

http://en.wikipedia.org/wiki/Decorator_pattern#Java

answered 2013-05-14T17:23:21.660

Your Answer