chapter 6:working with classes and their relationships

41
1 Working with Classes and Their Relationships

Upload: it-academy

Post on 11-Jan-2015

193 views

Category:

Technology


2 download

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

Page 1: Chapter 6:Working with Classes and Their Relationships

1

Working with Classes and TheirRelationships

Page 2: Chapter 6:Working with Classes and Their Relationships

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.

Page 3: Chapter 6:Working with Classes and Their Relationships

3

•Class compositions and associations• Class relationships•Multiplicities•Association navigation

Page 4: Chapter 6:Working with Classes and Their Relationships

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.

Page 5: Chapter 6:Working with Classes and Their Relationships

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.

Page 6: Chapter 6:Working with Classes and Their Relationships

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.

Page 7: Chapter 6:Working with Classes and Their Relationships

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.

Page 8: Chapter 6:Working with Classes and Their Relationships

8

Class Relationships

•Direct association•Composition association•Aggregation association•Temporary association

Page 9: Chapter 6:Working with Classes and Their Relationships

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.

Page 10: Chapter 6:Working with Classes and Their Relationships

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.

Page 11: Chapter 6:Working with Classes and Their Relationships

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.

Page 12: Chapter 6:Working with Classes and Their Relationships

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.

Page 13: Chapter 6:Working with Classes and Their Relationships

13

Page 14: Chapter 6:Working with Classes and Their Relationships

14

MULTIPLICITIES

Page 15: Chapter 6:Working with Classes and Their Relationships

15

One-to-One Multiplicity.

• All four relationship types may have a one-to-one multiplicity.

Page 16: Chapter 6:Working with Classes and Their Relationships

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.

Page 17: Chapter 6:Working with Classes and Their Relationships

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.

Page 18: Chapter 6:Working with Classes and Their Relationships

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.

Page 19: Chapter 6:Working with Classes and Their Relationships

19

Association Navigation

Page 20: Chapter 6:Working with Classes and Their Relationships

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.

Page 21: Chapter 6:Working with Classes and Their Relationships

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 */}

Page 22: Chapter 6:Working with Classes and Their Relationships

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}

Page 23: Chapter 6:Working with Classes and Their Relationships

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.

Page 24: Chapter 6:Working with Classes and Their Relationships

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.

Page 25: Chapter 6:Working with Classes and Their Relationships

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);}}

Page 26: Chapter 6:Working with Classes and Their Relationships

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.

Page 27: Chapter 6:Working with Classes and Their Relationships

27

Page 28: Chapter 6:Working with Classes and Their Relationships

28

Page 29: Chapter 6:Working with Classes and Their Relationships

29

Page 30: Chapter 6:Working with Classes and Their Relationships

30

Page 31: Chapter 6:Working with Classes and Their Relationships

31

Page 32: Chapter 6:Working with Classes and Their Relationships

32

Page 33: Chapter 6:Working with Classes and Their Relationships

33

Page 34: Chapter 6:Working with Classes and Their Relationships

34

Page 35: Chapter 6:Working with Classes and Their Relationships

35

Page 36: Chapter 6:Working with Classes and Their Relationships

36

Page 37: Chapter 6:Working with Classes and Their Relationships

37

Page 38: Chapter 6:Working with Classes and Their Relationships

38

Page 39: Chapter 6:Working with Classes and Their Relationships

39

Page 40: Chapter 6:Working with Classes and Their Relationships

40

Page 41: Chapter 6:Working with Classes and Their Relationships

41