Upload
cassie-honor
View
226
Download
0
Tags:
Embed Size (px)
Citation preview
Mark Dixon, SoCCE SOFT 131 Page 1
22 – Object Oriented Analysis, Design, and Programming
Mark Dixon, SoCCE SOFT 131 Page 2
Session Aims & Objectives• Aims
– To introduce the fundamental ideas of object orientation
• Objectives,by end of this week’s sessions, you should be able to:
– create and use an object class
Mark Dixon, SoCCE SOFT 131 Page 3
Evolution of SoftwarePressman (1992) page 5:
Mark Dixon, SoCCE SOFT 131 Page 4
Software CrisisCustomer (User) dissatisfaction:
• Over budget
• Late delivery
• Does not do what is required
• Poor quality– accuracy– reliability– maintainability– ease of use and learning
Pressman (1992) p. 18
Mark Dixon, SoCCE SOFT 131 Page 5
Example: Counter v1Option ExplicitDim tmpCount As Long
Private Sub Form_Load() tmpCount = 0 Me.lblCounter.Caption = tmpCountEnd Sub
Private Sub btnUp_Click() tmpCount = tmpCount + 1 Me.lblCounter.Caption = tmpCountEnd Sub
Private Sub btnDown_Click() tmpCount = tmpCount - 1 Me.lblCounter.Caption = tmpCountEnd Sub
Private Sub btnReset_Click() tmpCount = 0 Me.lblCounter.Caption = tmpCountEnd SubCounter v1
Mark Dixon, SoCCE SOFT 131 Page 6
Structured Paradigm• Program made up of
– data structures, and – routines (procedures and functions) that
process the data within those structures.
• Each routine should perform a single, clearly identifiable operation.
• Each routine should be self-contained
• Go to statements replaced by structures
• Abstract data type = structure + procedures
Mark Dixon, SoCCE SOFT 131 Page 7
Example: Counter v2Private Sub Form_Load() CounterReset CounterDisplayEnd Sub
Private Sub btnUp_Click() CounterUp CounterDisplayEnd Sub
Private Sub btnDown_Click() CounterDown CounterDisplayEnd Sub
Private Sub btnReset_Click() CounterReset CounterDisplayEnd Sub
Counter v2
Option ExplicitDim tmpCount As Long
Sub CounterDisplay() Me.lblCounter.Caption = tmpCountEnd Sub
Sub CounterReset() tmpCount = 0End Sub
Sub CounterUp() tmpCount = tmpCount + 1End Sub
Sub CounterDown() tmpCount = tmpCount - 1End Sub
Mark Dixon, SoCCE SOFT 131 Page 8
Object-Oriented Paradigm• A program is made up of a number of objects that
communicate with each other by passing messages
• Each object contains– attributes/properties that represent its state, and– operations/methods that represent its behaviour
• Objects often mirror the real world– Customers– Students– Patients
Mark Dixon, SoCCE SOFT 131 Page 9
Classes and Instances• Object Classes
– general descriptions of types of objects,e.g. student, product, customer, lecturer, and room.
• Object Instances– specific items of a given class, e.g.
• each of you could be an instance of the student class• Room 214 could be an instance of the room class• I could be an instance of the lecturer class• Bolt could be an instance of the part class
Mark Dixon, SoCCE SOFT 131 Page 10
Object Concepts - Implementation
• Properties – implemented as– data structures (variables, arrays, and types).
• Methods – implemented as either– a procedure (to perform some processing), or– a function (to return a value).
• Object oriented paradigm builds on (rather than replaces) the structured paradigm
Mark Dixon, SoCCE SOFT 131 Page 11
Class Diagrams• Used to describe structure of object classes:
Module
Code: stringTitle: string
GetTitle(): stringSetTitle(t: string)Count(): integer
Class Attributes/Properties
Class Operations/Methods
Class Name
Mark Dixon, SoCCE SOFT 131 Page 12
Implementation in VB• class module – special type of module that
defines an object class– Project menu, Add Class Module item
• Extends the record / structure / user defined data type,– which is used to store related data which may
be of different types.
• An object stores– data – but also provides methods for accessing and
manipulating that data.
Animation
Mark Dixon, SoCCE SOFT 131 Page 13
OOP: Animation
Mark Dixon, SoCCE SOFT 131 Page 14
Modules/Units• 1 Class per Module
– keeps logically related things together– makes programming easier– less errors
• Example: Counter– Counter class put in separate module– main (form) module uses counter module
Mark Dixon, SoCCE SOFT 131 Page 15
Example: Counter v3Option ExplicitDim tmpCounter As Counter
Private Sub Form_Load() Set tmpCounter = New Counter tmpCounter.Reset tmpCounter.Display Me.lblCounterEnd Sub
Private Sub btnUp_Click() tmpCounter.Up tmpCounter.Display Me.lblCounterEnd Sub
Private Sub btnDown_Click() …
Private Sub btnReset_Click() tmpCounter.Reset tmpCounter.Display Me.lblCounterEnd Sub
Private Sub Form_Unload(Cancel As Integer) Set tmpCounter = NothingEnd Sub
Counter v3
Option Explicit
Private mCount As Long
Public Sub Display(tmpLabel As Label) tmpLabel.Caption = mCountEnd Sub
Public Sub Reset() mCount = 0End Sub
Public Sub Up() mCount = mCount + 1End Sub
Public Sub Down() mCount = mCount - 1End Sub
Counter (class module)
Mark Dixon, SoCCE SOFT 131 Page 16
Things to Note• The dot notation is the same for both
records and objects.• A record variable may be accessed
immediately after definition whereas an object must first be ‘created’: Set tmpCounter = New Counter
Mark Dixon, SoCCE SOFT 131 Page 17
Benefits of OOP in code• Procedures and Functions are part of object
– encapsulation
• Related Data and Operations together
• Private keyword – restrict access to data
• Clearer code
• Less prone to error
Mark Dixon, SoCCE SOFT 131 Page 18
Why change?• It’s well established that program quality
improves as the semantic distance between the programming language and the real world problem language is diminished.
• It’s believed that the concept of communicating objects provides a better general framework for programming since it is closer to the real world situation than the structured paradigm.
Mark Dixon, SoCCE SOFT 131 Page 19
Public Code As StringPublic Title As String
Public Function GetTitle() As string Public Sub SetTitle(t As String) Public Function Count() As Integer
Implementing Class Diagrams
Module
Code: String[7]Title: String[25]
GetTitle(): stringSetTitle(t: string)Count(): integer
Mark Dixon, SoCCE SOFT 131 Page 20
Object Oriented Analysis• Look for nouns in text, either
– object classes, or– object properties
• Look for verbs in text,– object methods
The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks.The bar staff will then prepare the drinks and calculate the cost of the order.
Mark Dixon, SoCCE SOFT 131 Page 21
Identify all nouns and verbs
• Nouns: student's Union bar, computer system, drinks, student, bar, order, bar staff, cost.
• Verbs: recording the purchase, stagger, describe, prepare drinks, calculate cost
The students' Union bar needs a computer system for recording the purchase of drinks. Typically, a student will stagger to the bar and describe their order, consisting of one or (usually) more drinks.The bar staff will then prepare the drinks and calculate the cost of the order.
Mark Dixon, SoCCE SOFT 131 Page 22
Identify relevant nouns and verbs• What is relevant?
– depends on project scope, duration, budget
• Scenario 1: small project, limited automation– Nouns: drinks, order, cost– Verbs: describe, calculate cost
• Scenario 2: large project, high automation– Nouns: student's Union bar, drinks, student,
bar, order, bar staff, cost.– Verbs: recording the purchase, describe,
prepare drinks, calculate cost
Mark Dixon, SoCCE SOFT 131 Page 23
Scenario 1: detail• Nouns: drinks, order, cost
• Verbs: describe, calculate cost
Mark Dixon, SoCCE SOFT 131 Page 24
Tutorial Exercise: Counter• Learning Objective: The important thing to understand from
this exercise is that using classes allows you to make data private, and therefore prevents it being accidentally changed by other modules.
• Task 1: Get the Counter examples (1, 2, and 3) from the lecture working.
• Task 2: Modify your code (for v3) – add code to prevent the counter going below zero.
• Task 3: Modify your code (for v3) – add code to prevent the counter going above ten.
• Task 4: Modify your code (for v3) – add code for two additional properties max and min, that are used by the above limits.
Mark Dixon, SoCCE SOFT 131 Page 25
Tutorial Exercise: Bar• Task 1: Continue the analysis of the bar
example in the lecture, and implement a simple object oriented bar drinks calculation program.