Java Reference
In-Depth Information
Consider a defect that occurred at Oozinoz when a developer began modeling a new machine
that included a bar code reader for identifying tubs. After scanning a tub for its ID, the
developer set the location of tub b to machine m with the following code:
//tell tub about machine, and machine about tub
t.setMachine(m);
m.addTub(t);
CHALLENGE 10.3
Suppose that the object t represents tub T308 and that the object m represents
the machine Fuser-2101 . Complete the object diagram in Figure 10.5, showing
the effects of the code that updates the tub's location. What defect does this reveal?
Figure 10.5. When completed, this diagram will show the flaw in a developer's
code that updates a tub's location.
The easiest way to guarantee relational consistency is to pull the relational information back
into a single table managed by a mediating object. Instead of having tubs know about
machines and machines know about tubs, you can give all these objects a reference to a
mediator that keeps a single table of tubs and machines. The java.util package provides
the Map interface, along with several implementing classes, to store key/value pairs. Figure
10.6 shows a class diagram with a mediator in place. The Mediator class uses a Map object
to store the tub/machine relationship:
package com.oozinoz.machine;
import java.util.*;
import com.oozinoz.chemical.Tub;
public class Mediator
{
protected Map tubToMachine = new HashMap();
Search WWH ::




Custom Search