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 ?
You should ask yourself some fundamental questions before considering inheritance over composition in this case, and in general:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With