Sie sind auf Seite 1von 17

Object-Oriented Programming

Composition and
Aggregation

Dr. Volodymyr Voytenko


2020-01-03
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

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

One test contains collection of 1 … N questions :

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

Name Person Address

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.

Name Person Address Borrower Loan


The best classes candidates ?
Relationship between classes ?

2020-01-03
UML diagram:
Loan Borrower Project

Name Person Address


-firstName: String -name: Name -street: String
-mi: char -address: Address -city: String
-lastName: String -state: String
+Person() -zip: String
+Name() +Person(name: Name, address: Address)
+Name(firstName: String, +getName(): Name +Address()
mi: char, lastName: String) +seName(name: Name): void +Address(street: String, city: String,
+getFirstName(): String +getAddress(): Address state: String, zip: String)
+getMi(): char +setAddress(address: Address): void +getStreet(): String
+getLastName(): String +toString(): String +getCity(): String
+setFirstName(firstName: +getState(): String
String): void +getZip(): String
+setMi(mi: char): void +setStreet(street: String): void
+setLastName(lastName: +setCity(city: String): void
String): void Borrower +setState(state: String): void
+getFullName(): String +setZip(zip: String): void
-loan: Loan +getFullAddress(): String

+Borrower()
Loan +Borrower(name: Name, address: Address)
+getLoan(): Loan
+setLoan(loan: Loan): void
+toString(): String

Name Person Borrower Loan Address


2020-01-03
Implementation in Java
An aggregation/composition relationship is usually
represented as a data field in the aggregated class.
public class Name { public class Person { public class Address {
/** Data fields */ /** Data fields */ /** Data fields */
/** Constructors */ private Name name; /** Constructors */
/** Methods */ private Address address; /** Methods */
} }
/** Constructors */
/** Methods */
}

Composition Aggregation

Name Person Address


Inner Classes Translation
If Name or Address is used in the Person class only, they can
be declared as an inner class in Person. For example,
public class Person {
private Name name;
private Address address;
...

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:

public class Student { public class Course { public class Faculty {


/** Data fields */ /** Data fields */ /** Data fields */
private Course[] private Student[] private Course[]
courseList; classList; courseList;
private Faculty faculty;
/** Constructors */ /** Constructors */
/** Methods */ /** Constructors */ /** Methods */
} /** Methods */ }
}

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 */
} }

What is the best way to do it?

2020-01-03
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 don’t care about
polymorphism, the aggregation design gives more
flexibility because the classes are less dependent
using aggregation than using inheritance. Favor
aggregation over inheritance.
2020-01-03
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

2020-01-03
•?
Thank you!

2020-01-03

Das könnte Ihnen auch gefallen