Anchoring a Chain
When you write the getResponsible() method for MachineComponent, you have to consider that a MachineComponent object's parent might be null. Alternatively, you can tighten up your object model, insisting that MachineComponent objects have a non-null parent. To achieve this, add a parent argument to the constructor for MachineComponent. (You may want to throw an exception if the supplied object is null.) Also consider that an object will be the root—a distinguished object that has no parent. A reasonable approach is to create a MachineRoot class as a subclass of MachineComposite (not MachineComponent). Then you can guarantee that a MachineComponent object always has a responsible engineer if
The constructor(s) for MachineRoot ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access