Visual Basic An Object Oriented Approach 6 Object
Visual Basic: An Object Oriented Approach 6: Object Modelling
OOP – The Basics Revised n n n Classes – templates for objects Encapsulation – separation of internal workings from external interface Properties – attributes of an object Methods – operations defin. Validateed for a class of objects Messages – interactions between objects
Object Models n Real programs will involve large numbers of objects n n 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
Use-Case Diagrams ATM - Bank Machine n Accept Card Verify PIN Print Statement User (Actor) Deposit Cash Withdraw Cash n n 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
Class Model based on Use-Case Diagram n ATM Account Validate. User Print. Statement Do. Deposit Do. Withdrawal : ATM Balance PIN Owner Deposit Withdraw Statement : Deposit(50) Having identified classes, need to define… n n n Interactions between them Structure Class Interfaces : Account
Coding a Class n Need to… n Determine storage needs (member variables) n n n 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 n n n Properties (the obvious) Parameters to Subs and Functions (can apply multiple values at once) Results of Functions
Coding Classes n Start from class lowest in structure/hierarchy Likely that higher up classes will have these as components n Easier to test Build then test each class n First individually, then n With classes it interacts with, then n In sub-system Have test data ready n Best to consider testing during design phases n Store test data and revise/reuse as necessary n n n
Example – Bank/ATM n Assume two classes only ATM Class models Teller machine n Account class models individual account Start with Account class n From Use-Case point of view, ATM accesses Account and is therefore controller n Can build and test this class before moving on to ATM (which needs a working account to enable a full test) n Decide on tests necessary based on Use-Case diagram n Be prepared to change Use-Case, structure etc. as whole picture unfolds. n n
Account Class Private mvar. Balance As Currency Private mvar. PIN As Integer Private mvar. Owner As String ‘ Unrealistic, but will do for example Whole Property and Method code not Public Property Get Balance() As Currency… shown here. Public Property Get PIN() As Integer… See samples next, Public Property Let PIN(new. Value As Integer)… Public Property Get Owner() As String… and exercises in Public Property Let Owner(new. Value As String)… chapters 2 and 7 for Public Sub Deposit(amount As Currency)… complete code. Public Sub Withdraw(amount As Currency)… Mostly, the code is Public Function Statement() As String… Simple and obvious.
Account – Sample Property and Method Public Property Get Balance() As Currency Balance = mvar. Balance End Property Public Property Get PIN() As Integer PIN = mvar. PIN End Property Public Property Let PIN(new. Value As Integer) mvar. PIN = new. Value End Property Public Sub Deposit(amount As Currency) mvar. Balance = mvar. Balance + amount End Sub Read-only property Read-write Read-only property Method definition
Testing Account n Set A = New Account A. Owner = “Fred Bloggs” A. PIN = “ 1234” A. Deposit 500. 00 Print A. Statement n n n Statement for: Fred Bloggs Balance = £ 500. 00 A. Withdraw 50 Print A. Statement for: Fred Bloggs Balance = £ 450. 00 ‘ etc… In VB can use the Immediate Window to test a class n 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.
Continue Development n Can go up hierarchy to create next class (ATM) n n n 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
Example ATM Method Public Function Validate. User(A As Account) As Boolean Dim user. PIN As Integer user. PIN = Input. Box(“Enter PIN”) If A. PIN = user. PIN Then Set mvar. Current. Account = A Validate. User = True Else This method depends Validate. User = False on the Account A being End If Available. Success of End Property method leads to ATM Storing a reference to the Account.
Testing an ATM Method Set A = New Account A. Owner = “Fred Bloggs” A. PIN = “ 1234” Set ATM = New ATM Print ATM. Validate. User A ‘ VB creates an Input. Box() 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. 00 ATM. Print. Statement for: Fred Bloggs Balance = £ 100. 00
References n n A Reference is a variable that acts as an alias for an object VB uses references exclusively for interacting with objects n n n 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.
References and Objects Set A = New Account Assigns reference Creates object A Set A 2 = A A 2 Account Object
Collections n A Collection maintains a list of object references n n n 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 n Can create collections of collections
Collection Methods n Four main methods n n n 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 n n Objects can be retrieved by number Objects can be added with a Key (string), which acts as a textual index
A Collection Set C = New Collection Set A = New Account A. Owner = “Fred” C. Add A Set A = New Account A. Owner = “Mary” C. Add A, “Second account” Print C. Count 2 Print C(1). Owner Fred Print C(“Second Account”). Owner Mary
Collections and For Each… n Collections and objects introduce a new style of For. . Next loop For Each A In C: Print A. Owner: Next Fred Mary
- Slides: 20