Relationships Among Classes
Relationships Among Classes
Relationships Among Classes
Classes
Summary
Agenda
• Coupling, cohesion and modularity
• Dependency
• Association
• Aggregation
• Composition
• Inheritance
• Key Points
References
• Russ Miles and Kim Hamilton. Learning UML 2.0. O’Reilly, 2006.
Chapter 5
The strength of a class relationship is based on how dependent the classes involved in the relationship are on
each other
Coupling
Coupling is the measure of the degree of interdependence between the classes or
modules.
Causes of Coupling:
• Ideally, modules should depend only on the published interfaces of other modules and not
on their internals.
Coupling Effects
If two classes are tightly coupled, changes to one class will most likely affect the other class
• To increase cohesion, we would like that all the constituents contribute to some well-defined
responsibility of the module (or class).
• Highly cohesive modules (or classes) tend to be more reliable, reusable, and understandable than less
cohesive ones.
Modularity
A system’s functionality must be provided by several well-designed, cooperating modules.
• Each module must provide certain functionality that is clearly specified by an interface.
• The interface also defines how other components may interact or communicate with the module.
• In OO programming, the layers of GUI, business logic, and data are a consequence of the idea of
modularity and separation of concerns principle.
The 3-layer Architecture
Our OO apps will have this architecture
• In this example, the UserInterface is dependent on the BlogEntry class because it will need to read the
contents of a blog’s entries to display them to the user
• The UserInterface and BlogEntry classes simply work together at the times when the user interface
wants to display the contents of a blog entry.
A dependency implies only that objects of a class can work together; therefore, it is considered
to be the weakest direct relationship that can exist between two classes.
Association
Association means that a class will actually contain a reference to an object, or objects, of the other
class in the form of an attribute.
The whole (or the owner) and the part can exist
independently
An Author owns a collection of blogs
Composition
Composition is a specialized form of aggregation. In composition, if the parent
object is destroyed, then the child objects also cease to exist.
• Composition is a strong type of aggregation and is sometimes referred to as a “death”
relationship.
A BlogEntry is made up of an
Example: The relationship between a book and its Introduction and a MainBody
chapters.
Inheritance Definition
Inheritance is the mechanism of deriving a class from existing ones (super classes). The new class
inherits attributes and behavior from the super classes.
• Hence, inheritance facilitates Reusability and is the way the “is a …” relationship is modelled.
• It is the strongest relationship between classes (the highest coupling)
Polymorphism Definition
Polymorphism is the ability to present the same interface for differing underlying
forms (data types).
• Class Polymorphism:
• An object of a subtype can play the role of a superclass object.
• For example, a Dog object can be treated as an Animal Object
Polymorphism Definition
Polymorphism is the ability to present the same interface for differing underlying
forms (data types).
• Method Polymorphism:
• In a program that manipulates different geometric shapes, the
methods to draw them would all have different
implementations.
• However, when we have a shape object, we know we can call
the draw function to draw the shape.
• The same interface is presented for all the different shape data
types.
UML Class Diagram - Example
Revised version
Key Points
• Simplicity is most valuable quality feature
• Low coupling (Each class has few well-defined relationships with other classes)
• A class diagram is a type of static structure UML diagram that describes the structure of a system
by showing the system's classes, their attributes, operations, and the relationships among
objects.
Exercise
• Create a Class Diagram for the SIA system.