Search code examples
javac++mixins

Correct way to implement what used to be a Mixin in Java?


I am part of a project that is consolidating code written in different languages at different times into one single application.

My piece is going from C++ to Java. After reading the comments to this question: https://stackoverflow.com/questions/587458/implement-mixin-in-java I concluded that mixins in Java are not really possible without code generation schemes, and those will not be well received within the team I'm working on.

What is the correct way to implement what used to be a Mixin in C++ using vanilla Java? In particular, there used to be an MVC-type design where some Controllers had mix-and-match logic that used controller state. Controllers would inherit Mixins to gain this functionality in various permutations such that a linear inheritance path does not make much sense.

The best thing I can think of right now is a bunch of utility-type classes that contain the logic that used to be in the Mixins, and have controllers call their functionality while passing themselves in as references so that the utility classes can access their state. IMHO this really stinks and I'm hoping there is a better way.

So, what is the right way to design this in Java?


Solution

  • If you really want mixins, you can use a language like Scala which supports them (and runs on a JVM with Java code)

    Otherwise I would composition, which is not pretty either, but is simple.