composition and aggregation

17
Object-Oriented Programming Composition and Aggregation Dr. Volodymyr Voytenko 22-02-07

Upload: patel-prit

Post on 20-Dec-2015

234 views

Category:

Documents


1 download

DESCRIPTION

hdbshsd

TRANSCRIPT

Page 1: Composition and Aggregation

Object-Oriented Programming

Composition and Aggregation

Dr. Volodymyr Voytenko

23-04-18

Page 2: 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

23-04-18

Page 3: Composition and Aggregation

Inheritance (will be considered later)

+drive()

Vehicle

-sunRoof

Car Truck

•“Is-a “ relationship• Relationship between a class and its refined versions

23-04-18

Page 4: Composition and Aggregation

“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

Project update : more classes

23-04-18

Page 5: Composition and Aggregation

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.

One test contains collection of 1 … N questions :

23-04-18

Page 6: Composition and Aggregation

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!

23-04-18

Page 7: Composition and Aggregation

Aggregation & CompositionAggregation 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.

Name Address Person

Composition Aggregation

23-04-18

Page 8: Composition and Aggregation

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.

The best classes candidates ?

NameName LoanLoanPersonPerson BorrowerBorrowerAddressAddress

Relationship between classes ?

23-04-18

Page 9: Composition and Aggregation

UML diagram: Loan Borrower Project

NameName BorrowerBorrowerPersonPerson LoanLoan AddressAddress

Loan

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

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

Person -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 -firstName: String -mi: char -lastName: 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

23-04-18

Page 10: Composition and Aggregation

Implementation in JavaAn aggregation/composition relationship is usually represented as a data field in the aggregated class.

Name Address Person

Composition Aggregation

public class Name {

/** Data fields */

/** Constructors */

/** Methods */

}

public class Person {

/** Data fields */

private Name name;

private Address address;

/** Constructors */

/** Methods */

}

public class Address {

/** Data fields */

/** Constructors */

/** Methods */

}

Page 11: Composition and Aggregation

Inner Classes TranslationIf 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 { ... }}

23-04-18

Page 12: Composition and Aggregation

Implementation : Java class definitionTo represent a general binary relationship that describes an activity between two classes, we can use arrays:

public class Student {

/** Data fields */ private Course[]

courseList;

/** Constructors */

/** Methods */

}

public class Course {

/** Data fields */ private Student[]

classList;

private Faculty faculty;

/** Constructors */

/** Methods */

}

public class Faculty {

/** Data fields */

private Course[]

courseList;

/** Constructors */

/** Methods */

}

23-04-18

Page 13: Composition and Aggregation

Using Inheritance and AggregationIn 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

23-04-18

Page 14: Composition and Aggregation

Using Inheritance or Aggregation, cont.

public class Cylinder {

private Circle circle;

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

public class Cylinder extends Circle {

  /** Constructors */ 

/** Methods */

}

What is the best way to do it?

23-04-18

Page 15: Composition and Aggregation

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.

23-04-18

Page 16: Composition and Aggregation

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

23-04-18

Page 17: Composition and Aggregation

Thank you!

•?

23-04-18