13 Object Oriented Analysis Design and Programming Mark

13 – Object Oriented Analysis, Design, and Programming Mark Dixon, So. CCE SOFT 131 Page 1

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, So. CCE SOFT 131 Page 2

Evolution of Software Pressman (1992) page 5: Mark Dixon, So. CCE SOFT 131 Page 3

Software Crisis Customer (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, So. CCE SOFT 131 Page 4

Example 1: Counter v 1 Option Explicit Dim tmp. Count As Long Private Sub Form_Load() tmp. Count = 0 Me. lbl. Counter. Caption = tmp. Count End Sub Private Sub btn. Up_Click() tmp. Count = tmp. Count + 1 Me. lbl. Counter. Caption = tmp. Count End Sub Private Sub btn. Down_Click() tmp. Count = tmp. Count - 1 Me. lbl. Counter. Caption = tmp. Count End Sub Private Sub btn. Reset_Click() tmp. Count = 0 Me. lbl. Counter. Caption = tmp. Count End Sub Counter v 1 Mark Dixon, So. CCE SOFT 131 Page 5

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, So. CCE SOFT 131 Page 6

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, So. CCE SOFT 131 Page 7

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. • • Mark Dixon, So. CCE 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 SOFT 131 Page 8

Class Diagrams • Used to describe structure of object classes: Class Name Module Code: string Title: string Get. Title(): string Set. Title(t: string) Count(): integer Mark Dixon, So. CCE Class Attributes/Properties Class Operations/Methods SOFT 131 Page 9

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, So. CCE SOFT 131 Page 10

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, So. CCE SOFT 131 Page 11

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, So. CCE SOFT 131 Page 12

Example 3: Counter v 3 Option Explicit Counter (class module) Dim tmp. Counter As Counter Option Explicit Private Sub Form_Load() Private m. Count As Long Set tmp. Counter = New Counter tmp. Counter. Reset Public Sub Display(tmp. Label As Label) tmp. Counter. Display Me. lbl. Counter tmp. Label. Caption = m. Count End Sub Private Sub btn. Up_Click() tmp. Counter. Up tmp. Counter. Display Me. lbl. Counter End Sub Public Sub Reset() m. Count = 0 End Sub Public Sub Up() m. Count = m. Count + 1 End Sub Private Sub btn. Down_Click() … Private Sub btn. Reset_Click() tmp. Counter. Reset tmp. Counter. Display Me. lbl. Counter End Sub Public Sub Down() m. Count = m. Count - 1 End Sub Counter v 3 Mark Dixon, So. CCE SOFT 131 Private Sub Form_Unload(Cancel As Integer) Set tmp. Counter = Nothing Page 13

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 tmp. Counter = New Counter Mark Dixon, So. CCE SOFT 131 Page 14

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, So. CCE SOFT 131 Page 15

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, So. CCE SOFT 131 Page 16
![Implementing Class Diagrams Module Code: String[7] Title: String[25] Get. Title(): string Set. Title(t: string) Implementing Class Diagrams Module Code: String[7] Title: String[25] Get. Title(): string Set. Title(t: string)](http://slidetodoc.com/presentation_image_h2/4f4825bf7919d1e63e923e7fc5fc006b/image-17.jpg)
Implementing Class Diagrams Module Code: String[7] Title: String[25] Get. Title(): string Set. Title(t: string) Count(): integer Public Code As String Public Title As String Public Function Get. Title() As string Public Sub Set. Title(t As String) Public Function Count() As Integer Mark Dixon, So. CCE SOFT 131 Page 17

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, So. CCE SOFT 131 Page 18

Identify all nouns and verbs 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. • Nouns: student's Union bar, computer system, drinks, student, bar, order, bar staff, cost. • Verbs: recording the purchase, stagger, describe, prepare drinks, calculate cost Mark Dixon, So. CCE SOFT 131 Page 19

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, So. CCE SOFT 131 Page 20

Scenario 1: detail • Nouns: drinks, order, cost • Verbs: describe, calculate cost Mark Dixon, So. CCE SOFT 131 Page 21
- Slides: 21