Java features or mixins pattern?

Is there a way to imitate mixins or traits in java? basically, I need a way to do multiple inheritance, so I can add general business logic to several classes

+14
java mixins traits
Nov 04 '08 at 19:36
source share
8 answers

I would encapsulate all the business logic into a new BusinessLogic class and have every class that needs BusinessLogic to make calls to the class. If you need a single hierarchical hierarchy for your classes that call BusinessLogic calls, you will also have to create an interface ( BusinessLogicInterface ?)

In pseudo code:

 interface BusinessLogicInterace { void method1(); void method2(); } class BusinessLogic implements BusinessLogicInterface { void method1() { ... } void method2() { ... } } class User extends OtherClass implements BusinessLogicInterface { BusinessLogic logic = new BusinessLogic(); @Override void method1() { logic.method1(); } @Override void method2() { logic.method2(); } } 

This is not the prettiest implementation for working with the lack of multiple inheritance, and it becomes rather cumbersome when the interface has many methods. Most likely, you will want to try and reconfigure your code to avoid the need for mixing.

+12
Nov 04 '08 at 19:47
source share

Not the way you want to do it. Effective Java recommends that you "love composition over inheritance." This means that you are moving common logic to other classes and delegating. Here's how you get around the lack of multiple inheritance in java.

+17
Nov 04 '08 at 19:45
source share

Is the purist object in you today?

Think you could do with a little composite oriented programming?

Then you, sir, look for Apache Polygene (formerly Qi4J or Zest);)

+4
Nov 04 '08 at 20:26
source share

Java's answer to multiple inheritance is the ability to implement multiple interfaces. Of course, this means that you will receive method declarations, but not logic.

You can try mixins emulation by composition: your Java class can define member variables that represent other classes that execute some common business logic.

When developing Java classes, I did not find a flaw in C ++ style multiple inheritance to discourage the design of my architecture. You will find a way to achieve what you want to do.

+3
Nov 04 '08 at 19:44
source share

QI4J allows you to use mixins

+3
Oct 14 '10 at 17:48
source share

You can use the fact that interfaces allow nested classes (automatically public static) to support the standard implementation of interface methods encapsulated in the interface itself. That is, move the BusinessLogic class of example Alex B inside the interface.

This is similar to the way Scala generates JVM code for tags, as described here How are Scala compiled into Java bytecode?

The example will look like this:

 interface BusinessLogicInterface { void method0(); class DefaultImpl { private DefaultImpl() { } public static void method1(BusinessLogicInterface self) { ... } public static void method2(BusinessLogicInterface self) { ... } } void method1(); void method2(); } class User extends OtherClass implements BusinessLogicInterface { @Override void method0() { ... } @Override void method1() { BusinessLogic.defaultImpl.method1(this); } @Override void method2() { BusinessLogic.defaultImpl.method2(this); } } 

Please note that we pass the object of the interface type as the parameter "self". This means that business logic can use other abstract methods (method0). This can be very useful for creating a feature with abstract methods that are all orthogonal to each other and utility β€œextension” methods that can be implemented in terms of these orthogonal methods.

The disadvantage is that each interface must copy / paste the template delegation code. Another commonly used pattern in Java without this drawback (but with less connectivity and fewer OO methods for calling methods) is to create a plural name class as an interface containing static methods, which is used in the Collections utility class.

+2
May 2 '13 at 11:49
source share

Implementing simple mixin / traits support in java using CGLib / javassit is pretty simple. You can see an example here for a small example. A more complete, ready-to-use solution can be found: here

0
Oct 21 '11 at 11:18
source share

With Java-8, default interface methods have been added. This, along with multiple interface inheritance in Java, should allow some kind of mixin. Obviously, the interfaces must work independently. Thus, there will be significant limitations.

0
May 18 '15 at 9:37
source share



All Articles