5

I have two service beans called PowerUserManager and SimpleUserManager. Both @Service annotated classes have about 20% of the code in common.

I've built an inheritance tree with a common abstract class (BaseUserManager) to reduce redundancy between the two classes.

                               BaseUserManager
                                      |
                               ---------------
                               |             |
                       PowerUserManager  SimpleUserManager

Then in my @Controller or whatever client class, I use the @Autowired annotation to inject both PowerUserManager and SimpleUserManager and I use one of them depending on the instance of the User I'm dealing with.

I'm not comfortable using inheritance to factorize code especially in the service layer. Do you Spring fellows see a better way to do this ?

webpat
  • 1,769
  • 14
  • 19
  • Are the public interfaces the same, with differing behaviour, or do they have distinct methods that only apply to each type of user? – Romski Mar 01 '13 at 06:22
  • @Romski the PowerUserManager have distinct methods that only apply to the PowerUser – webpat Mar 05 '13 at 00:31

1 Answers1

3

You should ask yourself some fundamental questions before considering inheritance over composition in this case, and in general:

  1. Are all user managers a BaseUserManager? Is this a IS-A relationship in any possible case?
  2. Does it make sense to expose BaseUserManager public API everywhere where a user manager is invloved?
  3. Does BaseUserManager have a single responsibility?

If the answer is yes, then inheritance is the right way to go. Otherwise, you should probably redesign into several smaller components and treat PowerUserManager and SimpleUserManager as service facades.

Community
  • 1
  • 1
Szymon Jednac
  • 2,969
  • 1
  • 28
  • 41
  • What do you mean by single responsability for BaseUserManager ? – webpat Mar 05 '13 at 00:32
  • Does BaseUserManager follow the principles of OOP or is it a messy utility class, that just simplifies common use cases in other managers? For example, if BaseUserManager would be responsible for: permission & role management, registration, account activation, account listing and making coffee, then it might be a good idea, to delegate those tasks to a set of more specialized components. – Szymon Jednac Mar 05 '13 at 09:15