Managers manage workers. But who manages managers? The
answer: other managers. For example, in a particular company a level one
manager only manages workers. A level two manager manages workers and level one
managers. A level three manager manages workers and level one and two managers.
We could introduce Worker, Manager1, Manager2, Manager3 classes, etc. As
company grows, the number of levels of managers increases. Each time a new
level of managers is introduced, we will need to add a new class to our model.
Also, when an employee is promoted from level one to level two, say, we will
have to replace a Manager1 object with a Manager2 object.
We can simplify the design by using the the Composite
Design Pattern:
Note that as it stands, a level one manager could manage a level two manager. We could add a constraint to our model asserting that :
a manages b => a.level > b.level
Of course we would need to enforce this constraint in the implementation.