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.
John fitzpatrick
source share