Академический Документы
Профессиональный Документы
Культура Документы
Programming
Composition and
Aggregation
Objectives
To become familiar with the two
relationship types: aggregation and
composition
To design system by identifying the classes
and discovering the relationships among
classes
To implement Java code segment in
classes design that follow relationships
guidelines
15-04-27
Inheritance (will be
considered later)
Vehicle
+drive()
Car
-sunRoof
15-04-27
Is-a relationship
Relationship between a
class and its refined
versions
Truck
Has -a relationship!
1. Vehicle has: Engine, Transmission, etc Component
existence depends on aggregate!
2.
15-04-27
Aggregation
- Aggregation contains collections of
parts.
- Parts are living without whole
One test contains collection of 1 N questions :
15-04-27
Composition
- Much stronger then aggregation : a composite
entity doesnt exist, if all parts dont exist
- Parts are created with whole and die with whole!
15-04-27
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
Name
15-04-27
Aggregation
Person
Address
UML diagram:
Loan Borrower Project
Name
Person
-firstName: String
-mi: char
-lastName: String
-name: Name
-address: Address
+Person()
+Person(name: Name, address: Address)
+getName(): Name
+seName(name: Name): void
+getAddress(): Address
+setAddress(address: Address): void
+toString(): String
+Name()
+Name(firstName: String,
mi: char, lastName: String)
+getFirstName(): String
+getMi(): char
+getLastName(): String
+setFirstName(firstName:
String): void
+setMi(mi: char): void
+setLastName(lastName:
String): void
+getFullName(): String
Borrower
-loan: Loan
Loan
Name
15-04-27
Address
-street: String
-city: String
-state: String
-zip: String
+Address()
+Address(street: String, city: String,
state: String, zip: String)
+getStreet(): String
+getCity(): String
+getState(): String
+getZip(): String
+setStreet(street: String): void
+setCity(city: String): void
+setState(state: String): void
+setZip(zip: String): void
+getFullAddress(): String
+Borrower()
+Borrower(name: Name, address: Address)
+getLoan(): Loan
+setLoan(loan: Loan): void
+toString(): String
Person
Borrower
Loan
Address
Implementation in Java
An aggregation/composition relationship is usually represented as a data field in the aggregated class.
/** Constructors */
/** Methods */
}
Composition
Name
Aggregation
Person
Address
/** Constructors */
/** Methods */
}
/** Constructors */
/** Methods */
}
15-04-27
- Using inheritance
- Using aggregation
15-04-27
Using Inheritance or
Aggregation, cont.
public class Cylinder
{
private Circle
circle;
/** Constructors */
}
/** Methods */
/** Methods */
}
Using Inheritance or
Aggregation, cont.
1) Both designs are fine! (Check problem
domain!)
2) Which one is preferred?
If polymorphism is desirable, you need to use
the inheritance design. If you dont care about
polymorphism, the aggregation design gives
more flexibility because the classes are less
dependent using aggregation than using
inheritance.Favor aggregation over inheritance.
15-04-27
References
Liang, Introduction to Java Programming,(c)
2007-2015 Pearson Education, Inc.
Robert C. Martin, Designing object-oriented
C++ applications Using the Booch Method,
2005
UML basics: The class diagram. An
introduction to structure diagrams n UML 2,
Donald Bell, Architect, IBM Corporation, 2004
15-04-27
?
Thank you!
15-04-27