Anda di halaman 1dari 6

Structuring Class Diagram with Relationships:

Guidelines & Heuristics

Consider the previous Car Rental case study, we identified a list of candidate classes and methods from the following
problem statement:

ABC Car Rental Agency


A car rental agency rents cars to the public. A customer can rent a car if they have a valid driver's license and can
pay for the rental. A rental contract is drawn up to reflect the details. There are 3 classifications of cars based on
size. Within each classification, both wagons and sedans and both automatic and manual transmission versions are
maintained. A contract is terminated once the rental is complete. Rental of trucks is being considered.

Recall that, we had gone through the Textual Analysis process:

Textual Analysis

• √ Develop concise statement of requirements

• √ Identify classes
underline nouns
list and combine nouns
eliminate inappropriate candidate classes

• √ identify methods
underline verbs
list and combine verbs
eliminate inappropriate candidate methods

We had also identify additional methods for classes by going through external events

Object Identification Process

• √ identifying objects, methods, data


√ textual analysis
√ external events

Page 1 of 6
Structuring Class Diagram with Relationships.doc

We eventually obtained a list of classes and methods from the previous object identification process as follows:

Classes Methods

customer rent a vehicle


car terminate a contract
truck add a vehicle
contract delete a vehicle
update a vehicle

If we associate the methods to the classes, you will see the following classes as follows:
Classes Methods Data

Customer Rent a vehicle Driver license Number


Car Add Automatic?
Delete Wagon?
Update
Truck
Contract Terminate

The Equivalent Classes in a Class Diagram

Structural Analysis Process

• √ identifying objects, methods, data


√ textual analysis
√ external events
• organizing relationships among classes
associations
compositions
generalizations
• developing class diagram

Page 2 of 6
Structuring Class Diagram with Relationships.doc

Structural Analysis Process: Step I – identify associations


passing a message from object of class A to object of class B
object of class A creates an object of class B
object of class A receives a message which includes an object of class B as an argument
an attribute of an object of class A has values which are objects (or collections of objects) of class B

This heuristics does us always justify to add association between the objects, but only trigger us to think whether the
association should be linked between them.

Association Example I:
An object sends a message to another is not always to be essential for us to add association between them. Consider the
case below:

The Method RentVehicle in Customer object sends a mark_rented message to an object of class Car should be
considered to associate between them, because Customer is always required which car he rented. If the Customer
Object does not reference to the Car object, we would have to search the list of cars that the company had rented out
in an ad-hoc manner!

Passing a message from object of class A to object of class B


Method RentVehicle in Customer object sends a mark_rented message to an object of class Car

:Customer
:Car

rent_Vehicle
mark_rented
attributes
updated

Page 3 of 6
Structuring Class Diagram with Relationships.doc

Association Example II
Again does customer always need to “memorize" its own Contract? Does Customer object often need to reference (navigate)
from itself to the Contract to get retrieve information during the system’s operation? However, it will create the customer
object(s) instead of passing just passing messages to it. What is the implication of this? Let’s us elaborate this in the next
example.

Object of class A creates an object of class B


Method RentVehicle in Customer creates an object of Contract

Association Example III


We can see in this example, Customer can reference the information in Car through the object Contract or vise-versa. In
other words, the Car object can also write or read information by going through the object Contract. Thus, the object Contract
needed to be existed if an only if the relationship valid in between Customer and the Car. In this case, there should be an
association object (or sometime called linked object) Contract between Customer and Car.

Object of class A receives a message which includes an object of class B as an


argument
Method RentVehicle in Customer sends an object of Car to Contract

Page 4 of 6
Structuring Class Diagram with Relationships.doc

About the Associations Heuristics


No silver bullets! You should use these heuristics with caution and with the justifications behind. In short, an association
exists when some object of class A has to know something about an object of class B. Some object oriented beginners, often
like to include as many as association between objects that impose overhead into the system.

Structural Analysis Process


• √ identifying objects, methods, data

√ textual analysis
√ external events
• organizing relationships among classes

√ associations
compositions
generalizations

• developing class diagram

Structural Analysis Process: Step II & III – identify Composition and Generalizations
Composition
It is a “has a” relationship. If an instance of the object (whole) is deleted, the corresponding instances of the part(s) will also
be deleted. n this case, we discover this kind of relationship existed among the objects.

Generalization
Vehicle is a generalization of Car
Vehicle is a generalization of Truck

Vehicle can hold data like vehicle identification number, date purchased, is the vehicle currently rented and so on. Car can
hold data like number of passengers, is the car a station wagon and etc. in additional to the attributes hold by the more
general object – Vehicle. Likewise, the Truck can hold specialized data like weight capacity and etc. as shown in the Figure
below.

Page 5 of 6
Structuring Class Diagram with Relationships.doc

Recap Class Diagram Relationship


All 3 are relationships among classes
associations
communications between classes
compositions
“has a” relationship
whole-part relationship
generalizations
“is a” relationship
implemented with inheritance

Structural Analysis Process


• √ identifying objects, methods, data

– √ textual analysis

– √ external events
• organizing relationships among classes

– √ associations

– √ compositions

– √ generalizations

• developing class diagram

Developing Class Diagram

Page 6 of 6

Anda mungkin juga menyukai