简体   繁体   中英

Best way to break the inheritance hierarchy

I have legacy code that i'm refactoring. The way it currently stands is that there is a super class i'll call it SuperParent. Now anybody who needs some added functionality to SuperParent - extends SuperParent creates an interface for the specific functionality and then create a class subParent that extends SuperParent and implements this interface. This goes on and it's created a big sections of classes that are essentially extending from this SuperParent.

I don't know what the correct way to refactor this would be? Should it be just composition? My main goal to refactor this is to make the code cleaner- Because there are so many extensions of the SuperParent - it's difficult to see which class to extend. It's becoming unmanageable.

Template -> Strategy refactor:

Group methods by the fields in the super class they require. Refactor the fields and methods into strategy classes that are passed into the super class. Where methods are overridden pass in alternative implementations of the strategy classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM