Вы находитесь на странице: 1из 17

Object-Oriented

Programming

Composition and
Aggregation

Dr. Volodymyr Voytenko


15-04-27

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

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

15-04-27

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. Doesnt matter when
student or class deleting or creating.

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

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.
Person Address Borrower
Loan
Name

The best classes candidates ?


Relationship between classes ?
15-04-27

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.

public class Name {


/** Data fields */
/** Constructors */
/** Methods */
}

public class Person {


/** Data fields */
private Name name;
private Address address;

public class Address {


/** Data fields */
/** Constructors */
/** Methods */
}

/** Constructors */
/** Methods */
}

Composition

Name

Aggregation

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. Forpublic
example,
class Person {
private Name name;
private Address address;
...
class Name {
...
}
class Address {
...
}
}
15-04-27

Implementation : Java class


definition

To represent a general binary relationship that describes


an activity between two classes, we can use arrays:

public class Student {


/** Data fields */
private Course[]
courseList;

public class Course {


/** Data fields */
private Student[]
classList;
private Faculty faculty;

/** Constructors */
/** Methods */
}

/** Constructors */
/** Methods */
}

15-04-27

public class Faculty {


/** Data fields */
private Course[]
courseList;
/** Constructors */
/** Methods */
}

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, lets model the relationship
between Circle and Cylinder :

- Using inheritance
- Using aggregation
15-04-27

Using Inheritance or
Aggregation, cont.
public class Cylinder
{
private Circle
circle;

public class Cylinder


extends Circle
{
/** Constructors */

/** Constructors */
}

/** Methods */

/** Methods */
}

What is the best way to do it?


15-04-27

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

Вам также может понравиться