Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

SOLUTION 12.3....................................................................................................................................


A. A MachineComponent object may have an explicitly assigned responsible person. If
it doesn't, it passes the request to its parent:

public Engineer getResponsible()
{
if (responsible != null)
{
return responsible;
}
if (parent != null)
{
return parent.getResponsible();
}
return null;
}

B. The code for Tool.getResponsibility() reflects the statement that "tools are
always assigned to tool carts":

public Engineer getResponsible()
{
return toolCart.getResponsible();
}

C. The ToolCart code reflects the statement that "tool carts have a responsible
engineer":

public Engineer getResponsible()
{
return responsible;
}

SOLUTION 12.4....................................................................................................................................


Your solution should look something like Figure B.17. Your constructors should allow
Machine and MachineComposite objects to be instantiated with or without an assigned
engineer. Whenever a MachineComponent object does not have an assigned engineer, it
can get a responsible engineer from its parent.

Free download pdf