chapter 6:working with classes and their relationships
DESCRIPTION
Exam Objective 1.3 Describe, compare, and contrast class compositions, and associations (including multiplicity: one-to-one, one-to-many, and many-to-many), and association navigation.TRANSCRIPT
1
Working with Classes and TheirRelationships
2
Understanding Class Compositionsand Associations• Exam Objective 1.3 Describe, compare, and contrast
class compositions, and associations (including multiplicity: one-to-one, one-to-many, and many-to-many), and association navigation.
3
•Class compositions and associations• Class relationships•Multiplicities•Association navigation
4
Class Compositions and Associations
• Composition and association are the general terms used to describe a class relationship.
• An association or composition relationship is formed between two objects when one contains a reference to the other. The reference is often stored as an instance variable.
• The reference may be in one direction or bidirectional.
5
• An association relationship is a relationship of two objects where neither one directly depends on the other for their logical meaning.• For example, object A has an association relationship with object
B. If this relationship was lost, both objects would still retain the same meaning they previously had.• These relationships are considered weak. Objects in an
association relationship have no dependence on each other for the management of their life cycle.• In other words, the existence of an object is not tied to the
existence of the other object in the relationship.• The CarFactory object and CarFrame object have an association
relationship. If this relationship no longer existed, each object could continue to logically make sense and retain its original meaning on its own.
6
• Composition means that one object is composed of another.• The existence of object B is directly tied to the existence of object
A. When object A no longer exists, object B would also no longer exist.• A Car object and CarStatus object would be an example of a
composition relationship. • The Car object is composed-of the CarStatus object. Both objects
depend on this relationship to define their meanings. • The CarStatus object also depends on the Car object to maintain
its life cycle. When the Car object no longer exists, the CarStatus object would also no longer exist.
7
A composition will always be responsible for an object’s life cycle.
Composition relationships also represent a stronger relationship compared to an association.
Objects belonging to an association make more sense by themselves than objects of composition.
8
Class Relationships
•Direct association•Composition association•Aggregation association•Temporary association
9
Direct Association
• Direct association describes a “has-a” relationship. This is a basic association that represents navigability. • Direct association is a weak relationship and
therefore can be generalized to an association. • There is no life cycle responsibility and each object• in the relationship can conceptually be independent.
10
Composition Association
• A composition association can be described as object A is “composed-of” object B.• The containing object also has the responsibility of
managing the life cycle of the internal object.• It is possible for this object to pass the life cycle
management to another object. • Life cycle management means that the object composed
of the second object, or the containing object, must maintain a reference to the inner object, otherwise the Java Virtual Machine will destroy it.• If the containing object is destroyed, any objects that
compose it will also be destroyed.
11
Aggregation Association
• An aggregation association is a relationship that represents one object being part of another object.• Neither object depends on the other for its existence.
12
Temporary Association
• Temporary association is also known as a dependency. • Typically, a temporary association will be an object used as a
local variable, return value, or method parameter.• For example, a Car object may have a method called
startEngine that has a Key object as a parameter.• The Key object as a parameter would represent a temporary
association.
13
14
MULTIPLICITIES
15
One-to-One Multiplicity.
• All four relationship types may have a one-to-one multiplicity.
16
One-to-Many Multiplicity
• One-to-many relationships are created when one object contains a reference to a group of like objects.• The multiple object references are normally stored in an
array or a collection. • All four relationship types may be one-to-many.• A many-to-one relationship is also possible.
17
Many-to-Many Multiplicity
• Many-to-many relationships are only possible for aggregation associations, direct associations, and temporary associations.• Composition association is a strong relationship that implies a
life cycle responsibility for the object that composes it.
18
Association Navigation
• Association navigation is a term used to describe the direction in which a relationship can be traveled. • An object that is contained within another object is said to be
navigable if the containing object has methods for accessing the inner object.• Most relationships are navigable in one direction, but if both
objects contain references to the other, it is possible to have a bidirectional navigable relationship.
19
Association Navigation
20
Class Compositions and Associations in Practice• Exam Objective 3.3 Develop code that implements simple
class associations, code that implements multiplicity using arrays, and recognize code that implements compositions as opposed to simple associations, and code that correctly implements association navigation.
21
One-to-One Class Association
public class Truck {/* This is an example of a one-to-onedirect association */Trailer trailer;void setTrailer(Trailer t){trailer = t;}/** Remainder of Truck class would be here */}
22
One-to-Many Class Association
public class Car {Wheel[] wheel = new Wheel[4];void setWheels(Wheel w) {wheel[0] = w;wheel[1] = w;wheel[2] = w;wheel[3] = w;}// Remainder of Car class would be here}
23
Many-to-Many Class Association
• An array or collection should be a dead give away that you are looking at a *-to-many relationship. • If there is only one object and an array or collection, it
will be a one-to-many relationship. • If there are two arrays or collections with references
to each other, it will be a many-to-many relationship.
24
One-to-One Class Composition
public class Tire {TireAirPressure tireAirPressure;Tire(){tireAirPressure = new TireAirPressure();}}• The Tire object has life cycle management responsibilities• to the TireAirPressure object.• If the Tire object was destroyed, the TireAirPressure object
would also be destroyed.
25
One-to-Many Class Composition
public class SensorStatus {int status;public SensorStatus(int
newStatus) {status = newStatus;}}
public class CarComputer {SensorStatus[] sensorStatus = new
SensorStatus[5];public CarComputer() {sensorStatus[0] = new SensorStatus(1);sensorStatus[1] = new SensorStatus(1);sensorStatus[2] = new SensorStatus(1);sensorStatus[3] = new SensorStatus(1);sensorStatus[4] = new SensorStatus(1);}}
26
Examples of Association Navigationpublic class PinStripe {Color color = new Color(Color.blue);Color getColor(){return color;}}• any object that had access to the PinStripe object could use its
getColor method, which is considered a getter, to navigate to the Color object. • In this example, the navigation is only in a single direction.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41