Java Reference
In-Depth Information
package com.oozinoz.applications;
import com.oozinoz.machine.*;
public class AmbitiousMenu
{
public Engineer getResponsible(SimulatedItem item)
{
if (item instanceof Tool)
{
Tool t = (Tool) item;
return t.getToolCart().getResponsible();
}
if (item instanceof MachineComponent)
{
MachineComponent c = (MachineComponent) item;
if (c.getResponsible() == null)
{
if (c.getParent() != null)
{
return c.getParent().getResponsible();
}
}
}
return null;
}
}
You can clean up this code by applying the C HAIN OF R ESPONSIBILITY pattern.
CHALLENGE 12.2
Redraw the diagram in Figure 12.1, moving getResponsible() to
SimulatedItem and adding this behavior to Tool .
The client code is a lot simpler if it can ask any selectable item for its responsible engineer:
public Engineer getResponsible(SimulatedItem item)
{
return item.getResponsible();
}
The implementation of getResponsible() for each item is simpler, too.
CHALLENGE 12.3
Write getResponsible() for:
A. MachineComponent
B. Tool
C. ToolCart
Search WWH ::




Custom Search