Beruflich Dokumente
Kultur Dokumente
Composition and
Aggregation
2020-01-03
Inheritance (will be considered later)
Vehicle
•“Is-a “ relationship
+drive() • Relationship between a
class and its refined
versions
Car Truck
-sunRoof
2020-01-03
Project update : more classes
“Has -a “ relationship!
1. Vehicle has: Engine, Transmission, etc… Component
existence depends on aggregate!
2. Driver has Vehicle …. Component existence may or may
not depend on aggregate
2020-01-03
Aggregation
- Aggregation contains collections of parts.
- Parts are living without whole
Shared aggregation: M :N relationship between Class and Student. List of students for
a class is still changing. Doesn’t matter when student or class deleting or creating.
2020-01-03
Composition
- Much stronger then aggregation : a composite
entity doesn’t exist, if all parts don’t exist
- Parts are created with whole and die with whole!
2020-01-03
Aggregation & Composition
Aggregation and Composition are modeling the
has-a relationship. If an object is exclusively
owned by an aggregated object, the relationship
between the object and its aggregated object is
always referred to as composition.
Composition Aggregation
2020-01-03
Identifying classes and relationships
• Example 1. Loan Borrower Project
Different type of loan products are available to potential
borrowers. We need to design a management system of
receiving loans by different borrowers who satisfy specific
criteria. We need to keep track of borrowers individual
information and all updates about current and prospective
loans.
2020-01-03
UML diagram:
Loan Borrower Project
+Borrower()
Loan +Borrower(name: Name, address: Address)
+getLoan(): Loan
+setLoan(loan: Loan): void
+toString(): String
Composition Aggregation
class Name {
...
}
class Address {
...
}
}
2020-01-03
Implementation : Java class definition
To represent a general binary relationship that describes an activity
between two classes, we can use arrays:
2020-01-03
Using Inheritance and Aggregation
In general, the difference between inheritance and
aggregation is the difference between the “is-a”
relationship and the “has-a” relationship.
Sometimes, the choice between inheritance and
aggregation is not obvious. For example, let’s
model the relationship between Circle and
Cylinder :
- Using inheritance
- Using aggregation
2020-01-03
Using Inheritance or Aggregation,
cont.
public class Cylinder public class Cylinder
{ extends Circle
private Circle {
circle; /** Constructors */
/** Constructors */
/** Methods */
/** Methods */
} }
2020-01-03
Using Inheritance or Aggregation,
cont.
1) Both designs are fine! (Check problem domain!)
2020-01-03
•?
Thank you!
2020-01-03