Chapter 6:Working with Classes and Their Relationships

225 views
134 views

Published on

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.

Published in: Technology, Health & Medicine
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
225
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
11
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Chapter 6:Working with Classes and Their Relationships

  1. 1. Working with Classes and Their Relationships 1
  2. 2. Understanding Class Compositions and 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. 2
  3. 3. • Class compositions and associations • Class relationships • Multiplicities • Association navigation 3
  4. 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. 4
  5. 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. 5
  6. 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. 6
  7. 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. 7
  8. 8. Class Relationships •Direct association •Composition association •Aggregation association •Temporary association 8
  9. 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. 9
  10. 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. 10
  11. 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. 11
  12. 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. 12
  13. 13. 13
  14. 14. MULTIPLICITIES 14
  15. 15. One-to-One Multiplicity. • All four relationship types may have a one-to-one multiplicity. 15
  16. 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. 16
  17. 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. 17
  18. 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. 18
  19. 19. Association Navigation 19
  20. 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. 20
  21. 21. One-to-One Class Association public class Truck { /* This is an example of a one-to-one direct association */ Trailer trailer; void setTrailer(Trailer t){ trailer = t; } /* * Remainder of Truck class would be here */ } 21
  22. 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 } 22
  23. 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. 23
  24. 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. 24
  25. 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); } } 25
  26. 26. Examples of Association Navigation public 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. 26
  27. 27. 27
  28. 28. 28
  29. 29. 29
  30. 30. 30
  31. 31. 31
  32. 32. 32
  33. 33. 33
  34. 34. 34
  35. 35. 35
  36. 36. 36
  37. 37. 37
  38. 38. 38
  39. 39. 39
  40. 40. 40
  41. 41. 41

×