How to do this without multiple inheritance

I have a group of classes that extend one abstract class. A subset of these classes requires the identical execution of one of the methods, another subset of the classes requires a different implementation of the method, and a third subset requires another. There are about ten child classes in total, but only three possible implementations of one of the methods. (There are many other methods that implement classes that have nothing in common.)

I am trying to figure out how to do this. I think that what I would do in C ++ is multiple inheritance: create three classes that implement only this method, and then inherit them from the corresponding one of these three classes.

Is there any best practice for this in Java?

I looked at an intermediate layer of three abstract classes, between the main abstract class and the children. Each of the three is inherited from the main abstract class and implements this method. Then the children inherit from these three. But what I don't like about this is that if the other method is accompanied by the similar behavior of the “grouping” and it does not correspond to the three “middle level” classes? It will be ugly

Does that make sense? I am writing in a hurry ...

EDIT: So, 24 hours after I asked my question, I got about a dozen templates for investigation. I'm not sure yet that they are all official names for design patterns. But I'm going to study each and then report back (and choose the right answer). Pattens has suggested so far:

* Delegation * Bridge * Strategy * Composition * Decorator (if I was choosing on name alone, I choose this one) 

I also need to add that the method that is being implemented requires access to almost all private members of the class. So it will be of great importance in my choice.

+7
source share
5 answers

In fact, I feel the strategy pattern here.

You may have this method in the base class delegate for the strategy that is passed during construction. Subclasses simply take place in any of the sets of Strategies within the framework of their construction. Strategies themselves can be accessed either outside the classroom or domestically as private classes. This may result in fewer classes in your hierarchy.

Thus, there are additional odors, even with my proposed solution. You can think of a higher level with interfaces (as mentioned in other solutions) and composition only without inheritance. Over time, I came to the conclusion that the inheritance is NOT my friend. I am currently avoiding this where possible.

+7
source

Use delegation, not inheritance. Ask all classes in the same group to delegate a common helper object to implement your method.

+6
source

If you insist on allowing it through inheritance, the bridge template from the GoF book may help here (maybe it may or may not be a perfect fit, depending on what problems in the domain cause the separation into three implementations). Personally, I most likely will return the three implementations of the method to the helper class and forward the method calls from the classes (which JB Nizet correctly refers to delegation).

+3
source

An easy quick fire solution without using inheritance would be to divert 3 "common methods" elsewhere, and then simply repeat the method call.

If you want to do this in a beautiful OO way, take a look at the decorator template that can help here: create 3 standard classes that implement the three methods you choose, and then “decorate” them with other classes.

+1
source

First, consider replacing an abstract class with an interface. I do not know if it is possible in your case.

Secondly, you can remove the functionality of your grouping method by another class (developer). Create 3 different classes with a method implementation (or just 3 different methods in a certain class) and call them from child classes as needed.

Thus, the “grouping” method will be defined in each child class, but it will simply explicitly call one of the three possible implementations.

+1
source

All Articles