Tải bản đầy đủ (.pdf) (9 trang)

Enterprise Java and UML 2nd Edition PHẦN 6 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (196.62 KB, 9 trang )

Java Example
Person.java shows how the association relationship shown in Figure VG.3 between
Person and Vehicle can be implemented in Java. Each reference to a Vehicle object is
kept in a Vector.
Person.java
The Person class simply holds the vehicles for a person.
import java.util.*;
/**
* The Person class contains all data and logic for a person
* in the system.
*/
public class Person
{
public Vector vehicles = new Vector();
/** Adds a vehicle to this person. */
public void addVehicle(Vehicle v)
{
this.vehicles.addElement(v);
}
}
Guidelines
Association is the default long-term relationship between objects. If you are in doubt as
to which long-term relationship to use, use association.
Aggregation
Aggregation indicates a long-term relationship, with the additional restriction that
some of the objects are part of another object. It is this whole-part nature of the rela-
tionship that distinguishes aggregation from association.
UML Example
To continue the example, each Vehicle object may contain zero or one Engine objects.
There is a clear whole-part relationship, as the engine is part of the car or truck. Figure
VG.4 shows a modified association from Vehicle to Engine, with the hollow diamond


at the Vehicle indicating aggregation. The hollow diamond is always drawn next to the
enclosing whole.
50 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 50
Figure VG.4 Aggregation example.
Java Example
The following Java files show how the UML model in Figure VG.4 can be implemented
in Java. Only the files that have changed from the previous running example are
shown.
Vehicle.java
Vehicle no longer determines whether it is running or not. Instead, this behavior is
delegated to an Engine object.
/**
* The Vehicle class contains the data and behavior that
* is common to all Vehicles.
*/
public abstract class Vehicle
{
private Engine engine;
/** Sets the engine */
public void setEngine(Engine e)
{
this.engine = e;
}
/** Answers the noise made when the Vehicle goes.
Must be overridden by all concrete implementations
of Vehicle. */
Vehicle
+ go() : String
+ startEngine() : void

+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
+ loadCargo() : void
Engine
+ start() : void
+ stop() : void
+ isOn() : boolean
<<Interface>>
ICargoTransport
+ loadCargo() : void
Car
+ go() : String
Person
+ addVehicle(v : Vehicle) : void
1 * 0 *
0 1
1
Visual Glossary 51
267783 WS08.qxd 5/5/03 9:19 AM Page 51
public abstract String go();
/** Starts engine. */
public void startEngine()
{
if (this.engine != null)
{
this.engine.start();
}
}

/** Stops engine. */
public void stopEngine()
{
if (this.engine != null)
{
this.engine.stop();
}
}
/** Answers true if engine is started. */
public boolean isEngineOn()
{
if (this.engine != null)
{
return this.engine.isOn();
}
return false;
}
}
Engine.java
Engine.java provides very simple behavior for starting, stopping, and checking the
current value.
/**
* The Engine class contains the data and behavior for all engines
* for use with Vehicles.
*/
public class Engine
{
private boolean on;
/** Starts this engine. */
52 Visual Glossary

267783 WS08.qxd 5/5/03 9:19 AM Page 52
public void start()
{
this.on = true;
}
/** Stops this engine. */
public void stop()
{
this.on = false;
}
/** Answers true if the engine is running. */
public boolean isOn()
{
return this.on;
}
}
Guidelines
Aggregation requires a clear whole-part relationship. Any uncertainty about the need
for aggregation or ambiguity over which object is the whole and which is the part
should lead you to use association instead.
Composition
Composition is an even stronger relationship, with one object essentially owning the
other object or objects. The subordinate objects are created when the whole is created,
and are destroyed when the whole is destroyed. Also, an object cannot play the role of
a subordinate part in two composition relationships.
UML Example
Every engine contains many wheels, cogs, and gears that are integral and indivisible
parts of the greater whole. Figure VG.5 shows that each Engine object contains zero or
many Cog objects. The filled-in diamond next to the enclosing class indicates the com-
position relationship.

Java Example
The following Java file shows how the UML model in Figure VG.5 can be implemented
in Java. Only the files that have changed from the previous running example are
shown.
Visual Glossary 53
267783 WS08.qxd 5/5/03 9:19 AM Page 53
Figure VG.5 Composition example.
Engine.java
The Cog objects are created when the Engine is created, and become eligible for
garbage collection along with their enclosing Engine.
import java.util.*;
/**
* The Engine class contains the data and behavior for all engines
* for use with Vehicles.
*/
public class Engine
{
private boolean on;
private Vector cogs = new Vector();
public Engine()
{
this.cogs.addElement(new Cog());
this.cogs.addElement(new Cog());
}
/** Starts this engine. */
public void start()
{
Vehicle
+ go() : String
+ startEngine() : void

+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
+ loadCargo() : void
Engine
+ start() : void
+ stop() : void
+ isOn() : boolean
<<Interface>>
ICargoTransport
+ loadCargo() : void
Car
+ go() : String
Cog
Person
+ addVehicle(v : Vehicle) : void
1 * 0 *
0 1
1
1
*
54 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 54
this.on = true;
}
/** Stops this engine. */
public void stop()
{
this.on = false;

}
/** Answers true if the engine is running. */
public boolean isOn()
{
return this.on;
}
}
Guidelines
As with aggregation, when in doubt, do not use composition.
Dependency
Objects often need to use another object. An object may receive a reference as a param-
eter to a method, or it may create the object, use it, and lose it before the end of the cur-
rent method. The key idea is that the dependent object acquires, uses, and forgets the
object within a single method.
UML Example
Continuing the example, people use gas pumps to get gas, but most people do not keep
track of every pump that they have used. The Person object receives a reference to a
GasPump object as a parameter to the purchaseGas method. The reference is used
within the method; it is not kept. The resulting dependency relationship can be seen as
the dashed line from Person to GasPump in Figure VG.6.
Java Example
Person.java shows how the dependency relationship between Person and GasPump
can be implemented in Java.
Visual Glossary 55
267783 WS08.qxd 5/5/03 9:19 AM Page 55
Figure VG.6 Dependency example.
Person.java
Now, the Person class has a purchaseGas method that accepts a reference to a GasPump
object as a parameter.
import java.util.*;

/**
* The Person class contains all data and logic for a person
* in the system.
*/
public class Person
{
public Vector vehicles = new Vector();
/** Adds a vehicle to this person. */
public void addVehicle(Vehicle v)
{
this.vehicles.addElement(v);
}
public void purchaseGas(GasPump pump)
{
// use pump
// forget about pump
}
}
Vehicle
+ go() : String
+ startEngine() : void
+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
+ loadCargo() : void
Engine
+ start() : void
+ stop() : void
+ isOn() : boolean

<<Interface>>
ICargoTransport
+ loadCargo() : void
Car
+ go() : String
GasPump
Cog
Person
+ addVehicle(v : Vehicle) : void
+ purchaseGas(pump : GasPump) : void
1 * 0 *
0 1
1
1
*
56 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 56
Guidelines
Dependency should be used whenever an object is used and forgotten within a single
method.
Visual Glossary 57
267783 WS08.qxd 5/5/03 9:19 AM Page 57
267783 WS08.qxd 5/5/03 9:19 AM Page 58

×