Search code examples
javaoopinheritancedelegation

Java OOP design: what patterns can be used to design a hierarchy in which tasks/methods can be delegated between different instances of objects?


For example, if I have a person class, but each instance of a person can have 0 or more position or job - which have specific abilities and responsibilities - that he/she can switch between at will.


Solution

  • I think you can accomplish this with the Strategy Pattern. Basically, the strategy would be their job, but I would probably make a Worker or Employee class; not all Persons are workers.

    If a Person encapsulates the notion of applying a strategy to accomplish their tasks or responsibilities they can switch tasks or responsibilities on the fly (runtime).