20
Visual Basic: An Object Oriented Approach 6: Object Modelling

Visual Basic: An Object Oriented Approach 6: Object Modelling

Embed Size (px)

Citation preview

Page 1: Visual Basic: An Object Oriented Approach 6: Object Modelling

Visual Basic: An Object Oriented Approach

6: Object Modelling

Page 2: Visual Basic: An Object Oriented Approach 6: Object Modelling

OOP – The Basics Revised Classes – templates for objects Encapsulation – separation of

internal workings from external interface

Properties – attributes of an object Methods – operations definValidateed

for a class of objects Messages – interactions between

objects

Page 3: Visual Basic: An Object Oriented Approach 6: Object Modelling

Object Models Real programs will involve large

numbers of objects Will interact in various ways Will form logical structures May involve several levels of structure in

complex systems Key to maintaining coherence is in

building an object model that provides a ‘world view’ of the system

Page 4: Visual Basic: An Object Oriented Approach 6: Object Modelling

Use-Case Diagrams Used to obtain a high

level (user-level) picture of the system

Can be used in discussions with clients

Shows main interactions between system and users/external environment

ATM - Bank Machine

Accept Card

Verify PIN

Print Statement

WithdrawCash

DepositCash

User (Actor)

Page 5: Visual Basic: An Object Oriented Approach 6: Object Modelling

Class Model based on Use-Case Diagram

Having identified classes, need to define… Interactions between

them Structure Class Interfaces

ATM

ValidateUserPrintStatementDoDepositDoWithdrawal

Account

BalancePINOwner

DepositWithdrawStatement

:ATM :Account:Deposit(50)

Page 6: Visual Basic: An Object Oriented Approach 6: Object Modelling

Coding a Class Need to…

Determine storage needs (member variables) Include structural relationships – e.g. An Account

owns a collection of Transactions Implement Methods and Properties in terms

of accesses to member variables Consider mechanisms for getting info to

and from and object Properties (the obvious) Parameters to Subs and Functions (can apply

multiple values at once) Results of Functions

Page 7: Visual Basic: An Object Oriented Approach 6: Object Modelling

Coding Classes Start from class lowest in

structure/hierarchy Likely that higher up classes will have these

as components Easier to test

Build then test each class First individually, then With classes it interacts with, then In sub-system

Have test data ready Best to consider testing during design

phases Store test data and revise/reuse as

necessary

Page 8: Visual Basic: An Object Oriented Approach 6: Object Modelling

Example – Bank/ATM Assume two classes only

ATM Class models Teller machine Account class models individual account

Start with Account class From Use-Case point of view, ATM accesses

Account and is therefore controller Can build and test this class before moving on

to ATM (which needs a working account to enable a full test)

Decide on tests necessary based on Use-Case diagram

Be prepared to change Use-Case, structure etc. as whole picture unfolds.

Page 9: Visual Basic: An Object Oriented Approach 6: Object Modelling

Account Class

Private mvarBalance As Currency ‘ Unrealistic, but will do for examplePrivate mvarPIN As IntegerPrivate mvarOwner As String

Public Property Get Balance() As Currency…Public Property Get PIN() As Integer…Public Property Let PIN(newValue As Integer)…Public Property Get Owner() As String…Public Property Let Owner(newValue As String)…Public Sub Deposit(amount As Currency)…Public Sub Withdraw(amount As Currency)…Public Function Statement() As String…

Private mvarBalance As Currency ‘ Unrealistic, but will do for examplePrivate mvarPIN As IntegerPrivate mvarOwner As String

Public Property Get Balance() As Currency…Public Property Get PIN() As Integer…Public Property Let PIN(newValue As Integer)…Public Property Get Owner() As String…Public Property Let Owner(newValue As String)…Public Sub Deposit(amount As Currency)…Public Sub Withdraw(amount As Currency)…Public Function Statement() As String…

Whole Property and Method code not

shown here.See samples next,and exercises in

chapters 2 and 7 for complete code.

Mostly, the code is Simple and obvious.

Page 10: Visual Basic: An Object Oriented Approach 6: Object Modelling

Account – Sample Property and Method

Public Property Get Balance() As CurrencyBalance = mvarBalance

End Property

Public Property Get PIN() As IntegerPIN = mvarPIN

End Property

Public Property Let PIN(newValue As Integer)

mvarPIN = newValueEnd Property

Public Sub Deposit(amount As Currency)mvarBalance = mvarBalance +

amountEnd Sub

Public Property Get Balance() As CurrencyBalance = mvarBalance

End Property

Public Property Get PIN() As IntegerPIN = mvarPIN

End Property

Public Property Let PIN(newValue As Integer)

mvarPIN = newValueEnd Property

Public Sub Deposit(amount As Currency)mvarBalance = mvarBalance +

amountEnd Sub

Read-only property

Read-only property

Read-write property

Method definition

Page 11: Visual Basic: An Object Oriented Approach 6: Object Modelling

Testing Account

Set A = New AccountA.Owner = “Fred Bloggs”A.PIN = “1234”A.Deposit 500.00Print A.StatementStatement for: Fred BloggsBalance = £500.00A.Withdraw 50Print A.StatementStatement for: Fred BloggsBalance = £450.00‘ etc…

Set A = New AccountA.Owner = “Fred Bloggs”A.PIN = “1234”A.Deposit 500.00Print A.StatementStatement for: Fred BloggsBalance = £500.00A.Withdraw 50Print A.StatementStatement for: Fred BloggsBalance = £450.00‘ etc…

In VB can use the Immediate Window to test a class

Create an object Execute methods Print property values

(Using Print or ?)

Can also copy, the sequence of test statements, paste into Notepad or an editor, and save for re-testing if changes are made.

Page 12: Visual Basic: An Object Oriented Approach 6: Object Modelling

Continue Development Can go up hierarchy to create next

class (ATM) Now possible to test ATM since

Account class is in place ATM class must create an Account

object to interact with User-Interactions with ATM should

translate to ATM interactions with Account

Page 13: Visual Basic: An Object Oriented Approach 6: Object Modelling

Example ATM Method

Public Function ValidateUser(A As Account) As BooleanDim userPIN As Integer

userPIN = InputBox(“Enter PIN”)If A.PIN = userPIN Then

Set mvarCurrentAccount = AValidateUser = True

ElseValidateUser = False

End IfEnd Property

Public Function ValidateUser(A As Account) As BooleanDim userPIN As Integer

userPIN = InputBox(“Enter PIN”)If A.PIN = userPIN Then

Set mvarCurrentAccount = AValidateUser = True

ElseValidateUser = False

End IfEnd Property

This method depends on the Account A beingAvailable. Success ofmethod leads to ATM Storing a reference to

the Account.

Page 14: Visual Basic: An Object Oriented Approach 6: Object Modelling

Testing an ATM Method

Set A = New AccountA.Owner = “Fred Bloggs”A.PIN = “1234”Set ATM = New ATMPrint ATM.ValidateUser A‘ VB creates an InputBox() here to enter PIN number‘ ATM returns True or False depending on input to it.‘ Could now have ATM performing deposits and‘ withdrawals etc. e.g…ATM.Deposit 100.00ATM.PrintStatementStatement for: Fred BloggsBalance = £100.00

Set A = New AccountA.Owner = “Fred Bloggs”A.PIN = “1234”Set ATM = New ATMPrint ATM.ValidateUser A‘ VB creates an InputBox() here to enter PIN number‘ ATM returns True or False depending on input to it.‘ Could now have ATM performing deposits and‘ withdrawals etc. e.g…ATM.Deposit 100.00ATM.PrintStatementStatement for: Fred BloggsBalance = £100.00

Page 15: Visual Basic: An Object Oriented Approach 6: Object Modelling

References A Reference is a variable that acts as an

alias for an object VB uses references exclusively for

interacting with objects An object with no references to it is

destroyed automatically When an object is created, a reference to it

is assigned to an object variable As a consequence of this, an object can

have any number of references to it.

Page 16: Visual Basic: An Object Oriented Approach 6: Object Modelling

References and Objects

Set A = New AccountSet A = New Account

Creates objectAssigns referenc

e A Account Object

A2

Set A2 = ASet A2 = A

Page 17: Visual Basic: An Object Oriented Approach 6: Object Modelling

Collections A Collection maintains a list of

object references Therefore no need to maintain

individual reference variables to prevent objects from being destroyed

Acts as an unbounded array – no need to indicate how many elements to accommodate

A collection is itself an object Can create collections of collections

Page 18: Visual Basic: An Object Oriented Approach 6: Object Modelling

Collection Methods Four main methods

Add – adds an object reference to the collection Remove – removes a reference Count – returns number of references in the

collection Item – a virtual array in the collection – allows

access to object references Indexing

Objects can be retrieved by number Objects can be added with a Key (string), which

acts as a textual index

Page 19: Visual Basic: An Object Oriented Approach 6: Object Modelling

A Collection

Set C = New CollectionSet A = New AccountA.Owner = “Fred”C.Add ASet A = New AccountA.Owner = “Mary”C.Add A, “Second account”Print C.Count2Print C(1).OwnerFredPrint C(“Second Account”).OwnerMary

Set C = New CollectionSet A = New AccountA.Owner = “Fred”C.Add ASet A = New AccountA.Owner = “Mary”C.Add A, “Second account”Print C.Count2Print C(1).OwnerFredPrint C(“Second Account”).OwnerMary

Page 20: Visual Basic: An Object Oriented Approach 6: Object Modelling

Collections and For Each… Collections and objects introduce

a new style of For..Next loop

For Each A In C: Print A.Owner: NextFredMary

For Each A In C: Print A.Owner: NextFredMary