java - Inject functionality into two classes without common super class -


i know wanna sounds stupid listen:

i have abstract classes entity , player (extends entity) , non-abstract classes testplayer (extends player) , testmob (extends entity).

now facing following problem:

i want implement abstract methods in entity same functionality inside testplayer , testmob. theoretically create class testentity (extending entity) , make testplayer , testmob inherit it. testplayer couldn't inherit player anymore.

implementing functionality directly inside entity not option isn't intended sub-classes have functionality.

what considered acceptable solution other having duplicate code?

you use decorator pattern (https://en.wikipedia.org/wiki/decorator_pattern). have interface entity , 2 classes implementing it: concreteentity , decorator, concreteentity has default logic , decorator has reference entity , delegates method invocations. can extend decorator. instead of new testplayer(), have new player(new testentity(new concreteentity)))


Comments