Java Reference
In-Depth Information
There is a path —a series of object references—from a to c because a refers to b and b refers
to c . A cycle is a path in which a node appears twice. There would be a cycle of references in
this object model if the c Chemical object referred back to the a:Assay object.
Object models are directed graphs: An object reference has a direction. Graph theory usually
applies the term tree to refer to undirected graphs. However, a directed graph may be called a
tree if it
Has a root node that has no references to it
Has exactly one edge, or reference, to all other nodes
We can also express the second rule by saying that each node except the root has exactly one
parent —the node that refers to it.
The object model that Figure 5.3 depicts is a tree. For larger object models, it can be difficult
to tell whether the model is a tree. Figure 5.4 shows the object model of a factory, called
plant , that is a MachineComposite object. This plant contains a bay that has three
machines: m , n , and o . The object model also shows that the plant object's list of machine
components contains a direct reference to machine m .
Figure 5.4. An object model of a small fireworks factory
The plant object in Figure 5.4 does not contain a cycle, but note that it is not a tree, because
two objects refer to the Machine object m . The bay object model, viewed in isolation from
the plant object, is a tree.
Methods that work on composites may have defects if they assume that a composite is a tree.
Challenge 5.2 asked for a definition of a getMachineCount() operation. The Machine
class implementation of this operation as given in the solution to this challenge is arguably
correct:
Search WWH ::




Custom Search