mark dixon, socce soft 131page 1 13 – object oriented analysis, design, and programming

21
Mark Dixon, SoCCE SOFT 131 Page 1 13 – Object Oriented Analysis, Design, and Programming

Post on 19-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Mark Dixon, SoCCE SOFT 131 Page 1

13 – 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 1: 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

Mark Dixon, SoCCE SOFT 131 Page 7

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 8

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 9

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 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

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.

Mark Dixon, SoCCE SOFT 131 Page 12

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 13

Example 3: 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 14

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 15

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 16

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 17

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 18

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 19

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 20

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 21

Scenario 1: detail• Nouns: drinks, order, cost

• Verbs: describe, calculate cost