Association Classes

advertisement
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
1 More on Class Models
Stevens Chapters 6 More relationships between Classes
Some of the examples from The Rational Edge,
http://www.therationaledge.com/content/nov_03/t_modelinguml_db.jsp
1.1 Bi-directional relationships
An association is a linkage between two classes. Associations are assumed to be bi-directional – in
other words, both classes are aware of their relationship and of the other class – unless you qualify
the association as some other type of association. Below is an example of a bi-directional association
between the Flight class and the Plane class.
If no arrows are shown, they are implied to be present on both ends.
 The Plane class uses the role assignedFlights to associate itself with the Flight class.
assignedFlights is a pointer in the Plane class of type Flight.
o The multiplicity value next to the Plane class of 0..1 means that when an instance of a
Flight exists, it can either have one instance of a Plane associated with it or no Planes
associated with it (i.e., maybe a Plane has not yet been assigned to a flight).
o Also from the above, we see that a Plane knows about its association with the Flight
class.
o The assignedFlights is a pointer attribute of Plane what points to instances of
Flights. See the code below.
 The Flight uses the role assignedPlane to associate itself with a Plane. assignedPlane
is a pointer in the Flight class of type Plane.
o The multiplicity value next to the Flight class of 0..n means that when an instance of a
Plane exists, it can be associated with anywhere from no to many flights (e.g., it's a
©2011 Mike Rowe
Page 1
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
brand new plane may not yet be associated with a Flight) or with a lot of flights (e.g., the
plane has five different flights scheduled for its day’s worth of service).
o Note that we use the plural assignedFlights to indicate that a plane can have
multiple flights associated to it.
o The assignedPlane is a pointer attribute of Flight that points to a single instance of
a Plane. See the code below.

Note the assignedPlane attribute in the Flight.h auto generated code from Rose,
/// Flight.h--------------------------------------------------------#ifndef FLIGHT_H_HEADER_INCLUDED_B3378199
#define FLIGHT_H_HEADER_INCLUDED_B3378199
class Plane;
//##ModelId=490F1B9101B5
class Flight
{
public:
// delays the flight by a specified number of minutes
//##ModelId=490F1D2800BB
void delayFlight(int numberOfMinutes);
// returns the date-time of this flight
//##ModelId=490F1E40000F
Date getArrivalTime();
// set the pilot and copilot for a flight
//##ModelId=490F1E7E01B5
void assignFlightCrew(string pilot, string copilot);
private:
// The flight number
//##ModelId=490F1BAC01A5
int flightNumber;
// Flight departure date and time
//##ModelId=490F1C1D02AF
Date departureTime;
// flight time in minutes
//##ModelId=490F1C3A0242
int flightDuration;
// Airport fromwhich the airplane is flying
// codes
//##ModelId=490F1C62003E
string departingAirport;
// Airport to which the airplane is flying
// codes
//##ModelId=490F1C8A009C
string arrivingAirport;
©2011 Mike Rowe
Page 2
Uses the thre CHAR airport
Uses the thre CHAR airport
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
// A pointer of type Plane in the Flight Class that links a Flight Object
// to a plane object that will be flying the flight.
//##ModelId=490F201D0167
Plane *assignedPlane;
};
#endif /* FLIGHT_H_HEADER_INCLUDED_B3378199 */
/// Flight.cpp ----------------------------------------------------#include "Flight.h"
#include "Plane.h"
//##ModelId=490F1D2800BB
void Flight::delayFlight(int numberOfMinutes)
{
}
//##ModelId=490F1E40000F
Date Flight::getArrivalTime()
{
}
//##ModelId=490F1E7E01B5
void Flight::assignFlightCrew(string pilot, string copilot)
{
}

Note the assignFlight association in the Plane.h auto generated code from Rational.
/// Plane.h ----------------------------------------------------#ifndef PLANE_H_HEADER_INCLUDED_B337D2B3
#define PLANE_H_HEADER_INCLUDED_B337D2B3
class Flight;
//##ModelId=490F1F090203
class Plane
{
// the type of this airplane-- Like Boeing 787 Dreamliner
//##ModelId=490F1F1802CE
string airplaneType;
// Maximum airplan speed in knots
//##ModelId=490F1F2E01F4
int maxSpeed;
// Maximum range in knots (assumes empty plane with nuetral wind)
//##ModelId=490F1FB1038A
int maxDistance;
// A pointer of type Flight in the Plane Class that links a Plane Object to
// a flight object that the plane has been assigned to fly.
//##ModelId=490F201D0169
Flight *assignedFlights;
};
#endif /* PLANE_H_HEADER_INCLUDED_B337D2B3 */
©2011 Mike Rowe
Page 3
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
1.2 Uni-directional Relationships
A uni-directional association shows that two classes are related, but only one class knows that the
relationship exists. The below figure shows an example of an OverdrawnAccountsReport with a unidirectional association to the BankAccount class.


The uni-directional association includes a role name and a multiplicity value, but unlike a bidirectional association, the uni-directional association only contains the role name and
multiplicity value for the known class.
o In the figure, the OverdrawnAccountsReport knows about the BankAccount class, and
o the BankAccount class plays the role of "overdrawnAccounts."
o However, unlike a standard association, the BankAccount class has no idea that it is
associated with the OverdrawnAccountsReport.
The overdrawnAccount relationship would be implemented as a private attribute of
OverdrawnAccountReport (probably list/array/vector of pointers or references).
/// OverdrawnAccountReport ------------------------------------#ifndef OVERDRAWNACCOUNTSREPORT_H_HEADER_INCLUDED_B6F08005
#define OVERDRAWNACCOUNTSREPORT_H_HEADER_INCLUDED_B6F08005
#include "BankAccount.h"
//##ModelId=490F3C7D000F
class OverdrawnAccountsReport
{
public:
// Regenerated the OverdrawnAccount Report
//##ModelId=490F3CDD0148
void refresh();
private:
// Date and time when this report was generated.
//##ModelId=490F3C9402EE
Date generatedOn;
©2011 Mike Rowe
Page 4
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
//Pointer of type BankAccount in the OverdrawnAccountsReport
//Class that points to a list of those bank accounts that
//are overdrawn
//##ModelId=490F3F40005D
BankAccount *overdrawnAccounts;
};
#endif /* OVERDRAWNACCOUNTSREPORT_H_HEADER_INCLUDED_B6F08005 */
1.3 Aggregation
Aggregation – one class is part of another class.
 Denoted by an empty diamond on the Whole side.
 EX: A Company is an aggregation of several (1..*) Departments.
-theDepartment
Company
1



Department
1..*
An association with an aggregation relationship indicates that one class is a subordinate class
(or a part) of another class.
In an aggregation relationship, the child class instance can outlive its parent class. Thus, we
could split up a company by selling its Departments. The Departments would still exist, but the
Company would not.
The relationship –theDepartment allows the Company to know about the various Departments
that it has.
class Company
{
private Department departments[];
};
class Department
{
};

Note: This could be a bi-directional relationship if each Department knows about the parent
company.
1.4 Composition
In a composition, the Whole owns its parts. If the Whole is deleted, so are its parts – if the Whole is
copied, then so are its parts.
©2011 Mike Rowe
Page 5
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
-theSchedule
Student
1



Schedule
1
Composition is denoted by a filled diamond on the Whole side.
The multiplicity on the Whole MUST be 1. It makes NO sense that the parts are owned by more
than one Whole.
EX: a PeanutButterJellySandwich. Once it is made it is difficult to remove any of parts (Jelly,
PeanutButter, or Bread. The PeanutButterAndJellySandwich knows about its parts through
associations.
Bread
-theBread
1..2
-theJelly
PeanutButterAndJellySandwich
1
1
1
Jelly
1..*
-thePB
PeanutButter
1..*
EX: a Bicycle: has 2 wheels, the 2 wheels each have Spokes, a Hub, a Tube, Tire, Rim. If we sell the
bicycle, everything goes with it.
1
1
Seat
bicycle
2
1
1
Wheel
1
1
1
1
1
1
32
hub
tube
1
spokes
rim
1
tire
©2011 Mike Rowe
Page 6
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
Another Example: Walmart’s Computer in a box v. building a computer from scratch.
1.5 Qualified Associations



More details can be added to association.
Ex: if we have a chess board, we have 8 distinct rows by 8 distinct columns that will be
referenced as such (kings-knight-3).
Uses set notation for the enumerations.
ChessBoard
1
row:{1-8}
col:{1-8}
1
Square
There are 8 rows by 8 cols of squares on each chessboard
Note that the multiplicity of ChessBoard and Square is 1 to 1.


We can use other associations with qualifiers (for instance aggregation).
Notice: that the multiplicities are based on the qualifier 1:1 or in this case there is one set of 8
by 8 squares per ChessBoard.
1.6 Derived Associations
Derived associations are those that can be gleaned from other associations between objects.
 By knowing the Student is-taking Course and Teacher is-teaching-course we can derive an
association between Teach and Student.
Student
-theStudents
-isTaking
1..*
Course
1..*
-isTeaching
1..*
Teacher
-theTeacher
1..1
©2011 Mike Rowe
Page 7
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
Student
-theStudents
-isTaking
1..*
Course
1..*
-isTeaching
/teachesStudent
1..*
Teacher
-theTeacher
1..1



The black triangle ▲ indicates the direction of the association. It can be used with any
association name.
It is generally better not to add an actual association to a class model or implementation as it
opens up the possibility for objects becoming inconsistent.
EX: If we add a real association between Student and Teacher classes, we could add a studentteacher data relation without specifying Course information. If Teacher and Student is only a
derived association through Course, then we must always connect a Student and Teacher
through a Course – and there is no opportunity for inconsistency.
1.7 Reflexive Associations
0..*
Employee
-name : string
-position : int
-yearsAtCo : int
1
-manages
-manager
A class can also be associated with itself. In the above figure we have a Worker, who is an instance of
Employee and Manager who is an instance of Employee, thus we have two instances of employee
associated with each other.





This may not make sense at first, but remember that classes are abstractions.
In the below figure is another representation of the same model – is shows that how two
instances of the Employee class can be related reflexively through a manager/manages role.
When a class is associated to itself, this does not mean that a class's instance is related to itself,
but that an instance of the class (an object) is related to another instance of the class (another
object).
The relationship below means that an instance of Employee can be the manager of another
Employee instance.
However, because the relationship role of "manages" has a multiplicity of 0..*, an Employee
might not have any other Employees to manage.
©2011 Mike Rowe
Page 8
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
-manages
Worker
0..*
-manager
Manager
1
Employee
-name : string
-position : int
-yearsAtCo : int
1.8 Association Classes
Sometimes treating the association between two classes as a class is useful. This is what an
association class is.




In modeling an association, there are times when you need to include another class because it
includes valuable information about the relationship. For this you would use an association
class that you tie to the primary association.
An association class is represented like a normal class. The difference is that the association line
between the primary classes intersects a dotted line connected to the association class.
In the class diagram below, the association between the Flight class and the FrequentFlyer class
results in an association class called MileageCredit.
This means that when an instance of a Flight class is associated with an instance of a
FrequentFlyer class, there will also be an instance of a MileageCredit class.
MileageCredit
-bonusMiles : int = 0
-baseMiles : int = 0
Flight
-flightNumber : int
-departureTime : DateTime
-flightDuration : int = 60
-departingAirport : string = ""
-arrivingAirport : string
+delayFlight(in numberOfMinutes : int) : void
+getArrivalTime() : DateTime
+assignFlightCrew(in pilot : Employee, in copilot : Employee) : void
FrequentFlyer
0..*
-flights
0..*
-passengers
-firstName : string = ""
-lastName : string = ""
-frequentFlierNumber : string = ""
Example 2: Another option, in the below example of an association class is to create a separate class
and associate it with both Student and Course. Sometimes the naming of association classes deviate
from the normal naming conventions of using nouns and starting with an upper case character, instead
they use the name of the association, normally a verb.
If a student is no longer taking the course the is-taking Association class is no longer needed.
©2011 Mike Rowe
Page 9
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
1..*
Student
is-taking
4..6
Course
is-taking
Association Class
Student
grade : int
1..*
is-taking
4..6
Course
1
Alternative to
Association
Class
1
4..6
1..*
Grade
grade : int
©2011 Mike Rowe
Page 10
2/8/2016 12:23:00 PM
Notes: 007 Detailed Class Relationships
CS/SE3430 / CS5430 - Object-Oriented Analysis and Design
1.9 Constraints




Define conditions that must be met by an implementation.
Exclusive constraints means that there is an XOR association between two classes.
EX: a Copy can be either a Book or a Journal, but not both.
Can define very complex constraints with OCL (Object Constraint Language) (see example on
page 81).
1
1
LunchSpecial
Soup
1
{XOR}
The lunch special
permits either soup
or salad, BUT not both.
1
Salad
Many designers maintain that if your model has many constraints, this is a sign of a bad design and
will result in a system that is difficult to maintain and modules that have little reuse potential.
The use of OCL seems to becoming more common though because it can result in more precise models
and can be used to auto generate more code.
©2011 Mike Rowe
Page 11
2/8/2016 12:23:00 PM
Download