1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

26
1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes Objects and Classes

Transcript of 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

Page 1: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

1

StaffordshireUNIVERSITYSchool of Computing

Slide: 1Objects and Classes

Objects and Classes

Page 2: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

2

StaffordshireUNIVERSITYSchool of Computing

Slide: 2Objects and Classes

Lecture Objectives

Explain the idea of Classes and Objects

Explain how O-O systems differ from Structured systems in terms of internal functions.

Explain the idea of:

Encapsulation

Inheritance

Polymorphism

Explain the idea of

Inheritance/Generalisation

Association

Aggregation and Composition

Page 3: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

3

StaffordshireUNIVERSITYSchool of Computing

Slide: 3Objects and Classes

Classes and ObjectsOverview

Define what type of ‘building blocks’ are used to construct an O-O based system, and the way that these building blocks fit together i.e., they define the system’s structure.

Enable the unique features of O-O modelling:

Encapsulation

polymorphism

Inheritance.

Form the basis of many other types of UML diagram e.g., sequence diagrams and state machine diagrams.

Fundamental to O-O programming languages, therefore, they are the key link between the system design and its implementation.

Page 4: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

4

StaffordshireUNIVERSITYSchool of Computing

Slide: 4Objects and Classes

Classes and ObjectsOverview

-coverImage : object-artist : string-title : string-year : int

album

coverImage : object = def_reindeer_vol._1.jpgartist : string = Def Reindeertitle : string = Vol. 1year : int = 1982

Def Reindeer - Vol 1 : album

Class Object

Page 5: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

5

StaffordshireUNIVERSITYSchool of Computing

Slide: 5Objects and Classes

Classes and ObjectsAttribute Scope

link

-font : string = Tahoma-textDecoration : string = underlined-text : string-hyperTextRef : string = http:\\

Underlined fields are “classifier” or static fields

Page 6: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

6

StaffordshireUNIVERSITYSchool of Computing

Slide: 6Objects and Classes

Classes and ObjectsClass Packages

-coverImage : object-artist : string-title : string-year : int

<<business>>album

-font : string = Tahoma-textDecoration : string = underlined-text : string-hyperTextRef : string = http:\\

<<interface>>link

‹Guillemet quotes› are used to denote a “stereotype” – extra type information.Note that ‹‹business›› is often considered as a default and left out.

Page 7: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

7

StaffordshireUNIVERSITYSchool of Computing

Slide: 7Objects and Classes

Classes and ObjectsOperations

coverImage : object = def_reindeer_vol._1.jpgartist : string = Def Reindeertitle : string = Vol. 1year : int = 1982

Def Reindeer - Vol 1 : album

+create() : string+search() : string+delete()+readArtist()+changeArtist() : string+readTitle()+changeTitle() : string+readYear()+changeYear() : int+readCoverImage()+changeCoverImage() : object

-coverImage : object-artist : string-title : string-year : int

<<business>>album

Note that instance object class diagram only shows important information

Page 8: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

8

StaffordshireUNIVERSITYSchool of Computing

Slide: 8Objects and Classes

Classes and ObjectsEncapsulation

MessagesObject

Defined in the Class

Data

Page 9: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

9

StaffordshireUNIVERSITYSchool of Computing

Slide: 9Objects and Classes

Classes and Objects Structured Systems vs O-O Design

Page 10: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

10

StaffordshireUNIVERSITYSchool of Computing

Slide: 10Objects and Classes

Classes and ObjectsStructured Systems Design

Code: findAlbums

Goto to record 1.test if title contains “Vol. 1”

if so add it to the results list.Goto to record 2 … until all record tested

Data: albumsData: albums

Search for Albums Tab

Data: resultsData: results

Page 11: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

11

StaffordshireUNIVERSITYSchool of Computing

Slide: 11Objects and Classes

Classes and Objects OO System Design

2. addToAlbumList(artist=“My Artist”, title=“My Title”)

1. search(artist=“”, title=“*Vol 1*”, results=“Tab1:searchTab”)

Tab1:searchTab :album

Page 12: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

12

StaffordshireUNIVERSITYSchool of Computing

Slide: 12Objects and Classes

Classes and ObjectsFinding Class

Class Patterns

Abbott analysis, otherwise known as a noun analysis. Involves looking though the documentation already generated about the system, and a particularly good source of information are the use case scripts but may also include e.g.:

Specifications

Notes from interviews

Questionnaire results

Page 13: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

13

StaffordshireUNIVERSITYSchool of Computing

Slide: 13Objects and Classes

Classes and Objects Finding Classes: Use Case Scripts

Title: Login

Actors(s): Customer, Member, Membership Clerk, Finance Manager,

System Administrator.

Use case does: Allows actors to login in to wm-music.

Use case begins: when an actor wants to login to wm-music.

Use case ends: when an actor has successfully logged into the system, or quits

the login process.

Actor(s) System

1. clicks on a login link or button. 2. displays the login page.

3. enters their username and password. 4. verifies the information.

5. sets access permissions for which part of the website the actor is allowed to visit, and what options are available.

6. takes the actor to a default location in the system e.g., members are taken to the “Search of Album” page.

Alternative Actions

3. Actor has forgotten username or password – system sends username or password

to specified email address.

3. Actor does not have a user account for the site – actor may register on the system

[refer to use case: “Create User”].

5. Bad username or password – system displays error message – go to step

3.

Title: Login

Actors(s): Customer, Member, Membership Clerk, Finance Manager,

System Administrator.

Use case does: Allows actors to login in to wm-music.

Use case begins: when an actor wants to login to wm-music.

Use case ends: when an actor has successfully logged into the system, or quits

the login process.

Actor(s) System

1. clicks on a login link or button. 2. displays the login page.

3. enters their username and password. 4. verifies the information.

5. sets access permissions for which part of the website the actor is allowed to visit, and what options are available.

6. takes the actor to a default location in the system e.g., members are taken to the “Search of Album” page.

Alternative Actions

3. Actor has forgotten username or password – system sends username or password

to specified email address.

3. Actor does not have a user account for the site – actor may register on the system

[refer to use case: “Create User”].

5. Bad username or password – system displays error message – go to step

3.

Page 14: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

14

StaffordshireUNIVERSITYSchool of Computing

Slide: 14Objects and Classes

Classes and Objects Class Hierarchies

+create() : string+changeLastOrderDate() : int+changeTotalSpend() : decimal+delete()

-email : string-lastOrderDate : int-totalSpend : decimal

<<business>>customer

+create() : string+changeLastOrderDate() : int+changeTotalSpend() : decimal+changeEmail() : string+changeUserName() : string+changePassWord() : string+creditAcount() : decimal+debitAccount() : decimal+suspend()+re-instate()+delete()

-userName : string-passWord : string-email : string-lastOrderDate : int-totalSpend : decimal-currentBalance : decimal-susspended : bool

<<business>>member

+create() : string+changeFirstName() : string+changeSecondName() : string+changeUserName() : string+changePassWord() : string+changeEmail() : string+changeAcessRights() : string+delete()

-firstName : string-secondName : string-userName : string-passWord : string-email : string-acessRights : string

<<business>>employee

Page 15: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

15

StaffordshireUNIVERSITYSchool of Computing

Slide: 15Objects and Classes

Classes and Objects Class Hierarchies

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

+changeLastOrderDate() : int+changeTotalSpend() : decimal+changeEmail() : string+changeUserName() : string+changePassWord() : string+creditAcount() : decimal+cebitAccount() : decimal+suspend()+re-instate()

-userName : string-passWord : string-lastOrderDate : int-totalSpend : decimal-currentBalance : decimal-creditCardDetails : string-suspended : bool

<<business>>member

+changeFirstName() : string+changeSecondName() : string+changeUserName() : string+changePassWord() : string+changeEmail() : string+changeDataBaseAccessRights() : string

-firstName : string-secondName : string-userName : string-passWord : string-dataBaseAcessRights : string

<<business>>employee

+create() : string+delete()

-email : string

<<business>>user

Page 16: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

16

StaffordshireUNIVERSITYSchool of Computing

Slide: 16Objects and Classes

Classes and Objects Class Hierarchies

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

+changeLastOrderDate() : int+changeTotalSpend() : decimal+creditAcount() : decimal+debitAccount() : decimal+suspend()+re-instate()

-lastOrderDate : int-totalSpend : decimal-currentBalnace : decimal-suspended : bool

<<business>>member

+changeFirstName() : string+changeSecondName() : string+changeAccessRights() : string

-firstName : string-secondName : string-acessRights : string

<<business>>employee

+changeUserName() : string+changePassWord() : string+changeEmail() : string

-email : string-userName : string-passWord : string

<<business>>secureUser

+create() : string+delete()

-email : string

<<business>>user

Page 17: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

17

StaffordshireUNIVERSITYSchool of Computing

Slide: 17Objects and Classes

Classes and Objects Class Hierarchies

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

+creditAcount() : decimal+debitAccount() : decimal+suspend()+re-instate()

-currentBalnace : decimal-suspended : bool

<<business>>member

+changeFirstName() : string+changeSecondName() : string+changeAccessRights() : string

-firstName : string-secondName : string-acessRights : string

<<business>>employee

+changeUserName() : string+changePassWord() : string+changeEmail() : string

-email : string-userName : string-passWord : string

<<business>>secureUser

+create() : string+delete()

-email : string

<<business>>user

Page 18: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

18

StaffordshireUNIVERSITYSchool of Computing

Slide: 18Objects and Classes

Classes and Objects Polymorphism

Ordered>2Years:deleteCustomer

1. delete (lastOrderDate>2years)

+create() : string+deleteOldCustomer() : string+error() : string+delete() : string

<<system>>deleteCustomer

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

:customer

Page 19: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

19

StaffordshireUNIVERSITYSchool of Computing

Slide: 19Objects and Classes

Classes and Objects Polymorphism

Ordered>2Years:deleteCustomer

1. delete (lastOrderDate>2years)

+create() : string+deleteOldCustomer() : string+error() : string+delete() : string

<<system>>deleteCustomer

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

2. error (currentBalance>0)

1. delete (lastOrderDate>2years)

+creditAcount() : decimal+debitAccount() : decimal+suspended() : string

-currentBalance : decimal-creditCardDetails : string-suspended : bool

<<business>>member :customer

:member

Page 20: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

20

StaffordshireUNIVERSITYSchool of Computing

Slide: 20Objects and Classes

Classes and Objects Links: Association

<<business>>order

-Is for

1..50

-May have

0..*

<<business>>track

Page 21: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

21

StaffordshireUNIVERSITYSchool of Computing

Slide: 21Objects and Classes

Classes and Objects Links: Aggregation

<<business>>album <<business>>track

-Is on

1..*

-Has

1..*

Page 22: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

22

StaffordshireUNIVERSITYSchool of Computing

Slide: 22Objects and Classes

Classes and Objects Links: Composition

<<business>>artist

-Makes

1..*

-Is by

1

<<business>>album

Page 23: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

23

StaffordshireUNIVERSITYSchool of Computing

Slide: 23Objects and Classes

Classes and Objects Link

<<business>>artist

-Makes

1..*

-Is by

1-Is for 1..*

-May have 0..*-Places

0..1

-Is placed by

1

<<business>>customer

<<business>>member

<<business>>user

<<business>>secureUser

<<business>>order

<<business>>album <<business>>track

-Is on

1..*

-Has

1..*

Page 24: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

24

StaffordshireUNIVERSITYSchool of Computing

Slide: 24Objects and Classes

Classes and Objects Links

<<interface>>page

<<interface>>link

-textColour : string = Purple

<<interface>>visitedLink

-Has

0..1

-Is on 1..1

Make visitted links blueMake unvisited links purple

-textColour : string = Blue

<<interface>>unvisitedLink

<<interface>>tab

-Has

0..1

-Is on 1..1

-Is on

1..*

-Has

0..*

Page 25: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

25

StaffordshireUNIVERSITYSchool of Computing

Slide: 25Objects and Classes

Lecture Objectives

Explain the idea of Classes and Objects

Explain how O-O systems differ from Structured systems in terms of internal functions.

Explain the idea of:

Encapsulation

Inheritance

Polymorphism

Explain the idea of

Inheritance/Generalisation

Association

Aggregation and Composition

Page 26: 1 Staffordshire UNIVERSITY School of Computing Slide: 1 Objects and Classes.

26

StaffordshireUNIVERSITYSchool of Computing

Slide: 26Objects and Classes

Self-Test Questions

What is the difference between a class and an object?

What is the difference between an instance scoped attribute and a classifier scoped attribute?

What are class packages, and why are they useful?

What are class operations, and how are they invoked?

What do we mean by the term encapsulation, and why is this a useful idea?

What is the difference between the ways that data and code are treated in structured and O-O based systems?

What are the common ways of identifying the classes for a system design?

What do we mean by the terms inheritance, and what is another name for this term?

What do we mean by the term super class?

What is an abstract class?

What is the main benefit of using a class hierarchy?

What do we mean by the term polymorphism, and why is it a useful idea?

How are associations, aggregations and compositions different from each other?