0% found this document useful (0 votes)
4 views

Lecture21

Uploaded by

Aurangzaib Gill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture21

Uploaded by

Aurangzaib Gill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Aggregation,

Association,
Templates
CS 200
Learning Objectives
• Composition in context
• Aggregation
• Association
• Function templates
Composition vs Inheritance
• Objects inherit from other types in inheritance
• Object get composed of other types in
composition
• Functionally, aren’t they the same?
• Semantics:
• Inheritance is used in an is-a relationship
• Composition is used in a has-a relationship
• Liskov Substitution Principle (LSP)
• A derived class object should be usable instead of a
base class object
• Common interface, same behavior 3
A WebBrowser class with
a collection of Tab
objects

Further An EmailClient class


with a collection of
practice Email objects

A game Sprite with an


inventory of Items
(could be hierarchy)

4
Aggregation
• A “membership” relationship
• Weak ownership
• The aggregate object does not manage the part’s
lifetime
• The part(s) can exist independently of the aggregate
• Example:
• Team – Athlete
• Network – Computer
• Route – Vehicle
• Playlist – Songs
• Event – Calendar
5
Properties of Aggregation
• Part objects can be shared between multiple
aggregates
• An athlete can be part of multiple teams
• A computer can be connected to multiple networks
• Destroying the aggregate doesn’t destroy the
parts
• Tearing down a network doesn’t kill the computer
• Dissolving a team doesn’t exterminate the athletes
• Removing a part doesn’t impair the aggregate
• Removing a computer doesn’t eliminate the network
• Removing a vehicle from a fleet doesn’t render the
fleet non-existent 6
Code example
• A Playlist-Song example
• The same song can be added to
multiple playlists
• Removing a song doesn’t affect
the playlist

7
Summary
• The Song objects are created on the heap in
main()
• Pointers to some Song objects are added to a
Playlist object
• Song objects can be added/removed from the
Playlist object
• A Song object can be added to multiple Playlist
objects

8
A Portfolio class in which
different Stock objects can be
added
Android Newsfeed that aggregates
Articles from various sources
Further
A travel Itinerary class that
practic aggregates Flight, CarRental,
and Hotel booking objects
e A Quiz object aggregates some
questions from a Question pool

A Scene in a video game


aggregates multiple Sprite
objects, the Hero, and a
Background 9
UML Notation - Composition

Part2

1 1 1 1
Part1 Whole Part3
UML Notation - Aggregation

Part2

1 1 1 1
Part1 Whole Part3
Association
• Alternate: association separate from
aggregation and composition
• Not a part-whole relationship, but more of a
peer relationship
• Also, bidirectional
• Examples:
• Doctor-Patient
• Author-Book
• Actor-Movie
• Customer-Order
Let’s code

You might also like