Archive

Tag Archives: IDE

Java doesn’t provide a mixin functionality in its language core :-( . But you can think of a mixin as an interface together with an implementation and then it is possible to use the concept of a mixin in java and use it as a means of promoting code reuse.

Like everyone during development I find myself writing classes that share a certain set of functionalities. In other words they share the same small number of methods. If you prefer object composition over inheritance (as you should :) ) you define an interface and make all the classes implement this interface. But then another thing jumps up. You end up writing the same method bodies in every class. So you create one implementation of this interface and let every class use this implementation and just delegate the method calls. And now you’ve effectively created a simple mixin using the delegator pattern and one interface implementation and ended up with a lot of builerplate code. Like this:

// simple interface
public interface Description {

    String getName();

    String getDescription();

    String getVersion();

    void setName(String name);

    void setDescription(String description);

    void setVersion(String version);
}

// one implementation
public final class DescriptionImpl implements Description {
    private String name;
    private String description;
    private String version;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public String toString() {
        return "" + name + " \"" + description + "\" (" + version + ")";
    }
}

// mixin usage
public class Bean implements Description {
    // mixing in the description to this bean
    private final Description description = new DescriptionImpl();

    // HERE IS THE BOILERPLATE DELEGATION CODE
    public void setVersion(String version) { description.setVersion(version); }

    public void setName(String name) { description.setName(name); }

    public void setDescription(String description) { this.description.setDescription(description); }

    public String getVersion() { return description.getVersion(); }

    public String getName() { return description.getName(); }

    public String getDescription() { return description.getDescription(); }

    @Override
    public String toString() { return description.toString(); }
}

I know that this is a really simplistic example but just try to imagine some complex operations taking place in the default interface implementation or possibly some generic code. Wouldn’t it be nice if with the help of an IDE you would be able to write this and end up with the same thing as in the above code fragment?

public class Bean implements Description {

    @Mixin private final Description description = new DescriptionImpl();

// and here imagine the collapsed auto-generated builderplate code here
}

The mixin can depend on the object that mixes it in:

public interface Upcase {

    String toUpcaseString();
}

public final class UpcaseImpl implements Upcase {
    private Object object;

    public UpcaseImpl(Object object) {
        this.object = object;
    }

    public String toUpcaseString() {
        return object.toString().toUpperCase();
    }
}

public class Bean implements Upcase {
    @Mixin private final Upcase upcase = new UpcaseImpl(this);

// and here imagine the collapsed auto-generated builderplate code here
}

The technology is present in (all) modern IDEs but I haven’t seen anything like this yet. Has anybody seen something similar in their IDE? Another question is: does anyone care? (Or is it just me :-) )

Follow

Get every new post delivered to your Inbox.