-1

Working on a project so I'll try to generalize this.

Say I have an abstract class A with an abstract method method().

There are 4 subclasses of A: B, C, D, and E, but B and C have the same exact implementation of method(), and D and E have the same exact implementation of method(). How can I organize the code in such a way that minimizes duplicate code?

Hello
  • 219
  • 2
  • 7
  • Do you really need to use a template method? You can have two subclasses of A - one for each method. Then you can have B and C extend one, and D and E extend the other one. If you don't want more subclasses and can ditch the template method, you can go with a strategy pattern: a strategy for each method and then compose the classes with the corresponding strategy. – Ricardo Costeira Apr 21 '19 at 00:43

1 Answers1

0

put the common method in A or in a subclass of a. override the method in the cases where it is different.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90