Aula 8 - Building Objects
Aula 8 - Building Objects
Lesson 8
Building Objects
Composition:
Simplicity in Software:
• To ensure quality software, follow the rule of keeping things as simple as possible.
• Large software systems should be divided into manageable components for better
maintainability.
Subsystem Composition: “Stable complex systems are almost always composed of only a
few different kinds of subsystems, arranged in different combinations.”
Evolution of Stable Systems: “Stable systems that work have almost always evolved from
simple systems that worked.”
6
Modular vs. Integrated Systems: Stereo Example
Composition (Has-a): Integrated System Drawbacks:
• Imagine the stereo system as a single integrated black-box
system.
• If the CD player breaks and becomes unusable, the system must
be repaired.
• Repairing the entire system is more complicated and expensive.
• None of the other components can be used during the repair
process.
Benefits of Modular Systems:
• Modular systems allow individual components to be repaired or
replaced. An inheritance relationship.
• Association:
• Represents a collaboration between objects where individual parts are visible.
• The stereo system example used earlier is an instance of association.
• Aggregation:
• Represents a relationship where the whole is more visible than its individual parts.
• Both aggregation and association are examples of has-a relationships.
9
Types of Composition -
Aggregations
Aggregation as a Form of Composition
Definition of Aggregation:
• Aggregation means that a complex object is composed of other objects.
• Represents a has-a relationship where the whole is more prominent than the individual
parts.
TV Example:
• A TV is an aggregation of transistors, a picture tube, a tuner, and other components.
• People usually perceive the TV as a single, cohesive unit rather than focusing on the
individual components.
• When buying a TV, the salesperson doesn’t describe it as an aggregation of parts but
simply as a TV.
Intuitive Understanding:
• Aggregation is an intuitive form of composition because it’s how people typically
perceive complex objects in daily life.
10
Aggregation Example: Buying a Car
Car Purchasing Analogy:
• When buying a car, you aren’t focused on picking each
individual component (e.g., spark plugs, door handles).
• Instead, you choose a car as a whole, which is a
complex object composed of many simple and complex
parts.
• While you can select some optional features, the car is
still considered and purchased as a single unit (Figure).
Aggregation in Practice:
• Aggregation simplifies perception by combining
multiple components into a unified concept.
• Buyers typically don’t think about the individual parts
but instead about the final product that fulfills their
needs. An aggregation hierarchy for a car. 11
Types of Composition - Associations
Aggregation vs. Association
Aggregation:
• Represents relationships where only the whole is usually visible.
• The parts are abstracted away in favor of the overall entity.
Association:
• Shows both the whole and its individual parts distinctly.
• In the stereo example, each component (like speakers or a tuner) is visible and
connected to the whole via patch cords.
• Each stereo component has an interface that can be manipulated independently.
Designing Minimal Interfaces:
• Refer back to Chapter 2, ”How to Think in Terms of Objects,” for an example on
designing for minimal interfaces.
12
Aggregation vs. Association
Associations.
13
Using Associations and
Aggregations Together
Blurred Lines: Association vs. Aggregation
Distinguishing Association and Aggregation:
• In many cases, the dividing lines between association and aggregation are blurred.
• Interesting design decisions often revolve around whether to use associations or
aggregations.
• TV/VCR combinations offer the convenience of having both features in one module.
• If the TV breaks, the VCR becomes unusable as part of the integrated unit.
• In some cases, convenience might outweigh the risk of losing unit stability, depending
on the application and environment.
• In the TV/VCR example (Figure), the integrated unit provides a significant
convenience despite potential downtime.
Mixing Domains
Mixing domains is a design decision. If the convenience of a TV/VCR combination
outweighs the risk of downtime, then mixing domains may be the preferred choice.
17
Cardinality
Cardinality in Object Relationships
Definition of Cardinality:
• The number of objects participating in an association and whether the participation is
optional or mandatory.
Questions to Determine Cardinality:
• Which objects collaborate with other objects?
• How many objects participate in each collaboration?
• Is the collaboration optional or mandatory?
Example: Employee Class Relationships:
• Employee Class: Inherits from the Person class and has relationships with:
• Division
• JobDescription
• Spouse
• Child 18
Cardinality Example: Division and Job Description
Division Class:
Spouse Class:
Child Class:
20
Summary of Cardinality: Employee Class Relationships
Table: Represents the cardinality of the associations for the
previously discussed classes:
• Division: One-to-one, mandatory.
• Job Description: One-to-many, mandatory.
• Spouse: Zero or one, optional.
• Child: Zero to many, optional.
Figure: Shows the class diagram for this system, indicating
cardinality along association lines.
Cardinality Notation
The notation 0...1 means an employee can have either
zero or one spouse. The notation 0...n means an
employee can have any number of children from zero to an
unlimited number. Here, n represents infinity.
21
Multiple Object Associations
One-to-Many Association Example in Code
Representing One-to-Many Relationships: Classes that have one-to-many relationships, such as
Child and JobDescription, are represented by arrays in code.
i m p o r t j a v a . u t i l . Date ;
p u b l i c c l a s s Employee e x t e n d s P e r s o n {
p r i v a t e S t r i n g CompanyID ;
private String Title ;
p r i v a t e Date S t a r t D a t e ;
p r i v a t e Spouse spouse ;
private Child [ ] child ;
private Division division ;
private JobDescription [ ] jobDescriptions ;
Key Insights: Child[] child: Represents the one-to-many relationship between Employee and Child.
JobDescription[] jobDescriptions: Indicates an employee can hold multiple job descriptions. 22
Optional Associations
Handling Optional Associations in Code
Checking for Optional Associations:
• Your application should handle optional associations by verifying if the relationship is null.
• Code must check whether an associated object exists before invoking any methods on it.
Key OO Relationships:
• Inheritance, interfaces, composition, associations, and aggregations represent the primary
design relationships in OO systems.
25
Conclusion
Chapter Summary and Further Exploration
Summary of Composition Concepts:
• Explored finer points of composition and its two primary types: aggregation and association.
• Inheritance represents a new kind of already-existing object, while composition represents
interactions between different objects.
Further Exploration:
• This book provides a high-level overview of the object-oriented (OO) thought process.
• Seek other books for in-depth exploration of topics like UML and use cases, which have entire
texts devoted to them.
26
• May this overview inspire you to dive deeper into OO design concepts. Good hunting!
MC322 - Object Oriented Programming
Lesson 8
Building Objects