Oop lecture 2

57
CONNECTING THE DOTS Learning Outcomes At the end of this topic, the student is able to :- i. Model objects and classes using UML notation ii. Model relationships of objects and classes using UML notation iii.Produce UML class diagram >> Lecture 2 BITP 3113 Object Oriented Programming Sem I 2012/2013 Auhtored by Emma McKay-Fikri II. UML OBJECT ORIENTED MODEL

description

 

Transcript of Oop lecture 2

Page 1: Oop lecture 2

CONNECTING THE DOTS

Learning OutcomesAt the end of this topic, the student is able to :-i. Model objects and classes using UML notationii. Model relationships of objects and classes using UML notationiii. Produce UML class diagram

>>

Lectu

re 2

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

II. UML OBJECT ORIENTED MODEL

Page 2: Oop lecture 2

BLAST FROM THE PASTRecap

>>

Lectu

re 2

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

2

Page 3: Oop lecture 2

Principles of Object Oriented

• Identify classes, object and relationships

Abstraction

• Hiding information

Encapsulation

• Extending class definition

Inheritance

• Using the same name with different behavior

Polymorphism

>>

Reca

p

3BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 4: Oop lecture 2

Object

• Objects are things that make sense to the application.

• Classes are collections of objects with the similar states and behavior.

>>

Reca

p

4BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 5: Oop lecture 2

How to Identify Object?

• Object has Identity– What makes an object different from another object?

• Object has State– What is the data of the object?

• Object has Behavior– What the object can do?– What can we do with the object?

>>

Reca

p

5BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 6: Oop lecture 2

Can You Identify Objects?>

> R

eca

p

6BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 7: Oop lecture 2

Defining a Class

• In object oriented programming,– Objects are created– Classes are defined

• Object is an instance of a class

• A class is a general definition for a group of objects that has similar characteristics and behavior.

>>

Reca

p

7BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 8: Oop lecture 2

Designing a Class

• A class must have a Name– What is the unique name for a group of object?

• A class should have Instance variables– What the class should know?– What is the data or attribute of the class?

• A class should have Method– How to class act and react?– What is the name of the action and reaction?

>>

Reca

p

8BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 9: Oop lecture 2

Can You Identify Classes?>

> R

eca

p

9BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 10: Oop lecture 2

DIAGRAMMING CLASSLesson 1

>>

Lesso

n 1

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

10

Page 11: Oop lecture 2

Class Diagram

• Objects must interact in order to make the application function (and make sense thus reflects the real world model)

• Therefore, we need to model the relationship of the classes.

• In UML, a CLASS DIAGRAM is use to model the classes the relationships.

>>

Lesso

n 1

11

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 12: Oop lecture 2

Why Class Diagram?

• Class diagram represents the basics of Object Oriented System.

• It identifies what classes they are, how they interrelate and interact.

• It captures structure of object oriented system– Support architectural system

>>

Lesso

n 1

12

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 13: Oop lecture 2

What is UML?

• UML stands for Unified Modeling Language

• Its is a well-known and widely accepted modeling language for object oriented

• Further details, Google for UML.

>>

Lesso

n 1

13

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 14: Oop lecture 2

What is in a Class Diagram?>

> Le

sson 1

14

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

PropertyOwner

Token

Property

House

picks+selectedToken +player

1 1

buy

+ownProperty

+player

0..*

1

TitleDeed

has

+property

+titleDeed

1

1

Street Utility RailwayStationBuilding

Hotel

develop

+buildings 1..*builds on

1..*

Class

Multiplicity

Association

Role

Composition

Generalization

Aggregation

Page 15: Oop lecture 2

Steps to Produce Class Diagram

Identify objects and classes.

•Define instance variables and methods.

•Define access rights (public/private)

Connect the classes.

•Name the connection.

•Define degree of connection.

Refine the classes and

relationships. (Iterative process)

•Enrich classes•Enrich the relationship

A class diagram

is produced

>>

Lesso

n 1

15

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 16: Oop lecture 2

MODELLING CLASSLesson 2

>>

Lesso

n 2

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

16

Page 17: Oop lecture 2

Defining Class>

> Le

sson 2

17

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

-name : String-totalSupplyMoney : float

+setName(name:String):void+getName( ):String+setTotalSupplyMoney(totalSupplyMoney:float):void+getTotalSupplyMoney( ):float+updateTotalSupplyMoney(amount:float):void

Many objects(Player in black t- shirt, Player in blue t-shirt)

One class

Page 18: Oop lecture 2

UML Notation for Access Rights

Public +

Protected #

Package ~

Private -

>>

Lesso

n 2

18

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 19: Oop lecture 2

Class Representation in UML>

> Le

sson 2

19

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Class name

Instance variables

Methods

Compartment for instance variables and methods can be hide

Player

-name : String-totalSupplyMoney : float

+setName(name:String):void+getName( ):String+setTotalSupplyMoney(totalSupplyMoney:float):void+getTotalSupplyMoney( ):float+updateTotalSupplyMoney(amount:float):void

Page 20: Oop lecture 2

Designing Instance Variables

• A class should have Instance variables– What the class should know?– What is the data or attribute of the class?

• An instance variable tells what is the data and the type of the data a class should have or know.– At times, an instance variables tell how much it should exist

at one particular time.

>>

Lesso

n 2

20

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 21: Oop lecture 2

Modeling Instance Variables

• An instance variables should have the following:-– Name : What the data to be known as?– Type : What is the type of the data?– Visibility : Is it accessible to external of the class?– Multiplicity : How many of the data should exist?– Default value : What is the default value of the data?

• Format

>>

Lesso

n 2

21

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

visibility name : type [multiplicity] = default-value

Page 22: Oop lecture 2

Modeling Instance Variables

• In this case, the instance variables do not have multiplicity• This is considered as single instance variables

>>

Lesso

n 2

22

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

visibility(private)

- totalSupplyMoney : float = 0

name

type default-value

Page 23: Oop lecture 2

Designing Methods

• A class should have Method– How to class act and react?– What is the name of the action and reaction?

• A method will tell what it should do, what is the additional information to be supply and what it will return.

• A method is similar as function in structured programming.

• A good method will do only one task/job.

>>

Lesso

n 2

23

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 24: Oop lecture 2

Modeling Method

• A method should have the following– Name

• What the method wants to be known as?

– List of parameters • Additional data to be supply in order to make the method works

– Return type• Type of information to be returned to the caller of the method

– Visibility• Accessibility to external class

• Format

>>

Lesso

n 2

24

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

visibility name (parameter-list) : return-type

Page 25: Oop lecture 2

Modeling Method>

> Le

sson 2

25

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

+ updateSupplyMoney (amount:float) : void

visibilty name (parameter-list) return-type

Page 26: Oop lecture 2

CONNECTING OBJECTSLesson 3

>>

Lesso

n 3

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

26

Page 27: Oop lecture 2

Relationships

• One object is related to another in order to make it work in the environment.

• Example, Player picks Token• Two objects are interacting

– Player object– Token object

• Thus, we can say Player and Token has a relationship

>>

Lesso

n 3

27

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 28: Oop lecture 2

Association

• Association is general relations define for two objects

• If two objects are interacting, we can say the objects are associating.

• For example , the Player object associating with the Token object.

>>

Lesso

n 3

28

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 29: Oop lecture 2

Representing Association

• Association is represented using a solid line• It connects/relates two objects

• The association must be named

>>

Lesso

n 3

29

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player Tokenpicks

association

Page 30: Oop lecture 2

Association & OO Programming

• What does an association means in the context of programming?

• Objects are interacting.• Those interactions means the objects are

communicating.• Objects communicate through message passing.

• From programming point of view, message passing is invoking/calling method in the other object.

>>

Lesso

n 3

30

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 31: Oop lecture 2

Knowing the Other Side

• Association tells how one class knows the other class in order to perform its work.

• This ‘knowing’ needs a direction. The direction is known as Navigability in UML.

• Two types of navigability– Unidirectional association– Bidirectional association

>>

Lesso

n 3

31

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 32: Oop lecture 2

Unidirectional Association

• Unidirectional is represented using solid arrow in UML.

• This means, one end is aware of the existence of the other ends.

>>

Lesso

n 3

32

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Interpreted as :-

•Player has the responsibility to tell which token he pick but token no corresponding ability to tell which player it has.•Player is aware of token existence.

Player picks Token

Page 33: Oop lecture 2

Bidirectional Association

• Bidirectional is represent using solid arrow at both end.

• Both ends are aware of each other existence.

>>

Lesso

n 3

33

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player Tokenowning

Interpreted as :-

•Player has the responsibility to tell which token he owns and token has the ability to tell which player is owning it.•Player and Token is aware of each other existence.

Page 34: Oop lecture 2

NEW MEMBERLesson 4

>>

Lesso

n 4

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

34

Page 35: Oop lecture 2

Real World is Complex

• In real world object is complex.• An object may consist of many other objects.

• For example:-

• How would you define type for dateOfBirth?– Integer?– String?– Other types?

>>

Lesso

n 4

35

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

-name : String-dateOfBirth:?

Page 36: Oop lecture 2

Designing New Type

• Normally we used primitive type to define type for instance variables – int, float, double, char.

• Often, we used String for name, for example.– Well, String is never a primitive type.

• dateOfBirth consists of day, month, year.– day : int– month : could be int or String (depends on the format)– year : int

• Therefore, we need to define new class to represent dateOfBirth.

>>

Lesso

n 4

36

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 37: Oop lecture 2

Designing New Class

• Let’s design new class to represent dateOfBirth. • Let’s make it general and called it Date

– So that it would be a general class

>>

Lesso

n 4

37

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Date

-day:int-month:String-year:int

+createDate(day:int, month:String, year:int): String

Page 38: Oop lecture 2

Directed Association as New Member

• Player is associated with Date.• Thus, we can create a new object of Date (that is

dateOfBirth) as new member of Player.

>>

Lesso

n 4

38

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Date

-day:int-month:String-year:int

+createDate(day:int, month:String, year:int): String

Player

-name : String-dateOfBirth:Date

birth on

Page 39: Oop lecture 2

Degree of Participation

• Each instance variable will exist as one copy of member to the class.

• For example :-

• Each player will have only ONE dateOfBirth– Not other player will have more than one dateOfBirth,

right?

• This degree is known as multiplicity

>>

Lesso

n 4

39

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

-name : String-dateOfBirth:Date

Page 40: Oop lecture 2

Multiplicity

• Multiplicity indicates how many object participate in the relationship

>>

Lesso

n 4

40

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player Tokenpicks

11

Interpreted as :-One player object can picks only one token

object.

Player Propertybuy

0..*1

Interpreted as :-One player object buys no property or more

property.

Page 41: Oop lecture 2

Range of Multiplicity

• Single– Any single number– * indicate infinity

• Range– Example : 2...4, 0 ... *, 1 .. *

• A discrete set– Example : 2,4

>>

Lesso

n 4

41

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 42: Oop lecture 2

SUPER MODEL(ING)Lesson 5

>>

Lesso

n 5

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

42

Page 43: Oop lecture 2

Super Class & Sub Class

• The relationship between super class and sub class is modeled using generalization.

• Thus, model inheritance.

• In UML, generalization is using a solid line with hollow arrow head.

>>

Lesso

n 5

43

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 44: Oop lecture 2

Modeling Inheritance>

> Le

sson 5

44

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

PropertyOwnerSub class

Super class

is-a relationship

Interpreted as :-• PropertyOwner is a kind of Player.• PropertyOwner can develop property.• PropertyOwner inherits all public (package and protected) attributes

and methods from Player.• Every PropertyOwner is a Player.

Page 45: Oop lecture 2

Generalization>

> Le

sson 5

45

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Player

-name: String-selectedToken: Token-totalSupplyMoney: Float = 0

+setName(playerName: String): void+getName(): String+setSelectedToken(Token: token): void+getToken(): Token+setSupplyMoney(money: Float): void+getSupplyMoney(): Float+updateSupplyMoney(amount: Float): void

PropertyOwner

-house: House[1..*]-hotel: Integer[0..1]-utilities: Utility[0..*]-railroad: Railroad[0..*]

+developHouse(noOfHouses: Integer): void+developHotel(): void+mortgageHouse(street: Street): Float+mortgageHotel(street: Street): Float+mortgageUtilities(): Float+mortgageRailroad(): Float

Interpreted as :-

• Player can buy property.• When Player buy property, he

is a PropertyOwner.• Only PropertyOwner can

develop House or Hotel on the street.

• Only PropertyOwner can mortgage house, hotel, utilities and/railroad.

• PropertyOwner is-a Player.

Page 46: Oop lecture 2

RICHER RELATIONSHIPLesson 6

>>

Lesso

n 6

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

46

Page 47: Oop lecture 2

Variants of Relationships

• Relationships is not limited to directional association and generalization.

• Each type of relationship bring different meaning to the model.

• Some of the object may function if we remove it from the other object. Some may not.– Can you remove your legs and the legs still function by

it self?

>>

Lesso

n 6

47

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 48: Oop lecture 2

Advanced Relationship

• Therefor, a richer relationship is needed to describe the model precisely.

• This topic covers– Aggregation– Composition – Dependency

>>

Lesso

n 6

48

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 49: Oop lecture 2

Part and Whole

• Some object is made up from many objects.• In this case there are part-of object and whole-

object.• For example, a Car is made of Window, Door,

Tire, Engine etc.

>>

Lesso

n 6

49

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Whole Object Part-of Object

Page 50: Oop lecture 2

Modeling Aggregation

• Aggregation is the simplest part-of and whole object relationship.

• It is represented using solid line with hollow diamond head.

>>

Lesso

n 6

50

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 51: Oop lecture 2

Modeling Aggregation

• The Owner owns Station. • Therefore, Station is part-of Owner

>>

Lesso

n 6

51

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Owner

-name: String-capital: float-stationOwned: Station[1..*]

+setName(String name): void+getName(): String+setCapital(float capital): boolean+getCapital(): float

Station

-location: String

+setLocation(String location): void+getLocation(): String

owns

-stationOwned

1..*

Part-objectWhole-object Aggregation

Page 52: Oop lecture 2

Modeling Composition

• Composition is a stronger version of aggregation.

• It is represented by solid line and filled diamond-end

• The part may belong to only one whole object.• The part-object may live and die with the whole

object.

>>

Lesso

n 6

52

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 53: Oop lecture 2

Modeling Composition>

> Le

sson 6

53

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Property

-Name: String-Price: Float-Group: String

+DisplayProperty()

TitleDeed

-RentalRate: Float[1..*]-MortgageRate: Float[1..*]

+getRentalRate(noOfProperty: Integer): Float+getMortgageRate(noOfProperty: Integer): Float

has

+property

+titleDeed

1

1

Interpreted as :-

Player buy a Property and the TitleDeed is given to the player.When PropertyOwner mortgage the Property to the bank, the TitleDeed is given to the Bank.

The existance of TitleDeed is fully depends on Property. If the owner of the Property changed, so will the TitleDeed.

Page 54: Oop lecture 2

Modeling Dependency

• At time, we need to use other object but not as member of the class.

• An object can be passed as a parameter of a methods.

• This can be modeled using Dependency in UML

• Dependency is modeled using dotted arrow in UML.

>>

Lesso

n 6

54

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 55: Oop lecture 2

Modeling Dependency>

> Le

sson 6

55

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Interpreted as :-The Player is visible to push() method. Thus,

this is some kind of dependency.

Queue

-player: Player[0..*]

+push(player: Player): void+pop(): Player

Player

Page 56: Oop lecture 2

What Have You Learn Today?

Learning Outcomes Check Yourself Checked

i. Model objects and classes using UML notation

i. Create other classes in Monopoly Game using UML notation. (Complete with instance variables and methods)

ii. Model relationships of objects and classes using UML notation

i. Connects all classes using relationship.

ii. Give name and degree for the relationship.

iii. Produce UML class diagram

i. Refine classes, relationships and produce class diagram.

>>

Lectu

re 2

56

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

Page 57: Oop lecture 2

THAT’S WRAP FOR TODAYSee You in the Next Class

>>

Lectu

re 2

BITP 3113 Object Oriented ProgrammingSem I 2012/2013 Auhtored by Emma McKay-Fikri

57