You are on page 1of 7

Object Oriented Programming (CS304) VU

Head

Arm Ali Leg


2 2

1
Body

Example – Composition of Chair

Back

Chair

2 1 4
Arm Seat Leg

Composition is stronger relationship:


Composition is a stronger relationship, because
Composed object becomes a part of the composer
Composed object can’t exist independently

Example I

Ali is made up of different body parts

They can’t exist independent of Ali

Example II

Chair’s body is made up of different parts

© Virtual University of Pakistan 53


Object Oriented Programming (CS304) VU

They can’t exist independently

05.5. Aggregation

An object may contain a collection (aggregate) of other objects, the relationship


between the container and the contained object is called aggregation, Aggregation is
represented by a line with unfilled-diamond head towards the container

Example – Aggregation

Bed
1

Chair Room Table


* 1

1
Cupboard

Example – Aggregation

Garden Plant
*

Aggregation is weaker relationship

Aggregation is weaker relationship, because


• Aggregate object is not a part of the container
• Aggregate object can exist independently

Example I
Furniture is not an intrinsic part of room
Furniture can be shifted to another room, and so can exist independent of a
particular room

Example II
A plant is not an intrinsic part of a garden
It can be planted in some other garden, and so can exist independent of a particular
garden

http://www.codeproject.com/KB/cpp/oopuml.aspx

54 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

Lecture No.06

06.1. Class Compatibility


A class is behaviorally compatible with another if it supports all the operations of the
other class. Such a class is called subtype. A class can be replaced by its subtype.
Derived class is usually a subtype of the base class. It can handle all the legal
messages (operations) of the base class. Therefore, base class can always be replaced
by the derived class.

Examples
Child class also includes characteristics of its base class.

Shape

color
vertices

move
setColor
draw

Circle Triangle
Line
radius angle
length
draw
draw draw
computeArea
getLength computeArea

All the three derived class are behaviourally compatible with base class.

© Virtual University of Pakistan 55


Object Oriented Programming (CS304) VU

File

size

open
print

ASCII File PS File


PDF File
… …

print
… print
print

Wherever the file class is it can be replaced by any of its child classes.

06.2. Polymorphism
It is also essential component of object oriented modeling (paradigm).
In general, polymorphism refers to existence of different forms of a single entity. For
example, both Diamond and Coal are different forms of Carbon.

06.3. Polymorphism in OO Model

In OO model, polymorphism means that different objects can behave in different


ways for the same message (stimulus). Consequently, sender of a message does not
need to know exact class of the receiver.
Sender sends message to receiver and appropriate method is called on receiver side.

Example – Polymorphism

draw
View Shape
draw

Line Circle Triangle


draw draw draw

56 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

Shape class hierarchy shape is base class and there are three child classes line circle ,
triangle. View send draw method to shape class and draw is called according to the
nature of actual object present.

print
Editor File

print

ASCII File PDF File PS File

print print print

Editor sends message print to file class and print is called based on the actual child
object of file class message is same and appropriate execution will be done.

06.4. Polymorphism – Advantages


Messages can be interpreted in different ways depending upon the receiver class
New classes can be added without changing the existing model

draw Shape
View
draw

Square Line Circle Triangle

draw draw draw draw

In general, polymorphism is a powerful tool to develop flexible and reusable systems

06.5. Object-Oriented Modeling an Example

Problem Statement
Develop a graphic editor that can draw different geometric shapes such as line, circle
and triangle. User can select, move or rotate a shape. To do so, editor provides user
with a menu listing different commands. Individual shapes can be grouped together
and can behave as a single shape.

© Virtual University of Pakistan 57


Object Oriented Programming (CS304) VU

Identify Classes
Extract nouns in the problem statement
Develop a graphic editor that can draw different geometric shapes such as line,
circle and triangle. User can select, move or rotate a shape. To do so, editor provides
user with a menu listing different commands. Individual shapes can be grouped
together and can behave as a single shape.

Eliminate irrelevant classes


Editor – Very broad scope. But it is the name of overall system and we are going to
model it so we will not make its object. For example if we are going to model
computer we will not make its object but its components however if it is component
of some other system then it will behave as an object. So it is marked as irrelevant.
User – Out of system boundary, it is interacting with the system from outside of the
system.
Add classes by analyzing requirements
Group (of shapes) – required to behave as a shape so it should behave as an object in
our system

“Individual shapes can be grouped together and can behave as a single shape”

View – graphic editor must have a display area to show the shapes. We made this
object using domain knowledge.

• Shape
• Line
• Circle
• Triangle
• Menu
• Group
• View

So we have the following classes,

Shape G roup

Line Menu

Circle
View

Triangle

58 © Virtual University of Pakistan


Object Oriented Programming (CS304) VU

Finding Associations:

Next step is to find associations between the objects.

Identify Associations

Find relationships between objects,

1. Extract verbs connecting objects,

“Individual shapes can be grouped together”

• Group consists of lines, circles, triangles


• Group can also consists of other groups (Composition)

Line, circle and triangle have composition relationship.

2. Verify access paths

a. View contains (draws) shapes

• View contains lines


• View contains circles
• View contains triangles
• View contains groups

So there is Aggregation relationship between shapes and View.

Menu sends message to View

So there is Simple One-Way Association relationship between Menu and View.

Identify Attributes of the identified objects

Extract properties of the object,

a. From the problem statement


Properties are not mentioned
b. From the domain knowledge

• Line
i. Color
ii. Vertices
iii. Length
• Circle
i. Color
ii. Vertices
iii. Radius
• Triangle

© Virtual University of Pakistan 59

You might also like