object-oriented design and high-level programming … · chapter 9 object-oriented design and...

50
Chapter 9 Object-Oriented Design and High-Level Programming Languages

Upload: phungkiet

Post on 12-Apr-2018

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

Chapter 9

Object-Oriented Design

and High-Level

Programming Languages

Page 2: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

Chapter Goals

• Distinguish between functional design and object-

oriented design

• Describe the stages of the object-oriented design

process

• Apply the object-oriented design process

• Name, describe, and give examples of the three

essential ingredients of an object-oriented language

• Understand how the constructs of top-down and object-

oriented design are implemented in programming

languages

2

Page 3: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

3

9.1 OO Methodology

OO Design

A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects

Object

A thing or entity that makes sense within the context of the problem

E.g., a student, a car, the time, the date

Page 4: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

4

Object-Oriented Design

World View of OOD

Problems are solved by

– isolating the objects in a problem,

– determining their properties and actions

(responsibilities), and

– letting the objects collaborate to solve a

problem

What? Say again!

Page 5: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

5

Object-Oriented Example

You and your friend are making dinner

Objects: you, friend, dinner

Classes: you and friend are people

People have name, eye color, …

People can shop, cook, …

Instance of a class: you and friend are instances of class People, you each have your own name and eye color, you each can shop and cook

You collaborate to make dinner

Page 6: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

6

Object-Oriented Lingo

Class = description of a group of similar objects

Object = instance of a class = concrete example of an object in the class

Classes contain fields that represent:

• properties (name, eye color) and • behaviors (responsibilities) (shop, cook) of the

class

Method = A named algorithm that defines behavior (shop, cook)

Page 7: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

7

Two types of design

Top-Down Design

decomposes problems into tasks

Object-Oriented Design

decomposes problems into

collaborating objects

Yes, but how?

Page 8: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

8

OOD Steps

– isolate the real-world objects in the problem

– abstract the objects with like properties into groups (classes)

– determine the responsibilities of the group in interacting with other groups

Page 9: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

9

OOD Example

Think of design as a mapping from real

world objects to classes of objects

birth

date

marriage

date

dog's

birth date

Date class

Objects Classes of objects

Page 10: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

10

OOD Example

class Date

dogBirthdate

myBirthdate

marriageDate

Description Instances

Page 11: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

11

OOD Example

Date's

Actions in

real world

?

We call an object's interactions

with other objects its

responsibilities

Create itself

Know the state of its fields

Compare itself to another date

Return a date a number of days hence

Page 12: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

12

Responsibilities become methods

associated with the classes and objects

class Date

getMonth

getDay

getYear

dogBirthdate.getMonth()

dogBirthdate.getMonth()

dogBirthdate.getMonth()

myBirthdate.getDay()

Etc.

marriageDate.getYear()

Etc.

OOD Example

Page 13: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

Do not confuse the “responsibilities” of real-

life people with the “responsibilities” of the

objects representing those people!

Example: the zoo-keeper

• In real-life, (s)he has the responsibility to feed

the animals

• In an OO program: zooKeeper.SSN(), zooKeeper.listOfAnimalsCaredFor(), etc.

13

Page 14: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

14

OOD Methodology

Four stages to the decomposition process

– Brainstorming to locate possible classes

– Filtering the classes to find duplicates or

remove unnecessary ones

– Scenarios are tried to be sure we understand

collaborations

– Responsibility algorithms are designed for all

actions that classes must exhibit

Page 15: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

15

Design tool: CRC Cards

CRC cards are a notational device to record information

about a class, what is must do and with whom it must

collaborate

Page 16: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

16

OOD Example

Let’s examine the problem-solving process for creating an address list

Brainstorming and filtering

– Circling the nouns and underlining the verbs is a good way to begin

Page 17: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

17

OOD Example

Page 18: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

18

OOD Example

Can you think of any other useful responsibilities?

Page 19: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

19

OOD Example

Can you think of any other useful responsibilities?

Page 20: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

20

CRC Cards

How is this class different from Name and Person?

Page 21: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

21

Responsibility Algorithms

Person Class

Initialize

name.initialize()

Write "Enter phone number; press return."

Get telephone number

Write "Enter email address; press return."

Get email address

Print

name.print()

Write "Telephone number: " + telephoneNumber

Write "Email address: " + emailAddress

Tells name to initialize itself

Tells name to print itself

Page 22: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

22

Responsibility Algorithms

Name Class

Initialize

"Enter the first name; press return."

Read firstName

"Enter the last name; press return."

Read lastName

Print

Print "First name: " + firstName

Print "Last name: " + lastName

Page 23: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

23

Object Oriented Problem Solving

and Implementation Phases

Page 24: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

SKIP 9.2 Translation Process

24

Page 25: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

25

9.3 Programming Language

Paradigms

Imperative Paradigm

Program describes the processing

Declarative Paradigm

Program describes the results

Each of these major paradigms have distinct

subparadigms

Page 26: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

26

Imperative Languages

• Procedural

– Characterized by sequential instructions

– A program in which statements are grouped into a hierarchy of subprograms

– Fortran, C, C++

• Object-oriented model

– Program consists of a set of objects and the interactions among the objects

– Python, Java, Smalltalk, Simula

Page 27: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

27

Imperative Languages

C++ is as a procedural language with some object-oriented features

Java is an object-oriented language with some procedural features

Page 28: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

28

Declarative Languages

• Functional– Based on the mathematical concept of a

function– Lisp, Scheme, and ML

• Logic– Based on principles of symbolic logic– Types of statements

• declares facts about objects and relationships

• defines rules about objects

• asks questions about objects

– PROLOG

Page 29: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

29

Scheme – Everything is a

function!

#;> (* 3 4)

12

#;> (+ (* 5 4)(+ 1 4))

25

#;> (length '(2 4 6 8 10))

5

#;> (max 2 5 1 3)

5

Page 30: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

30

Scheme

#;> (define factorial

#;> (lambda(n)

#;> (if

#;> (= n 0)

#;> 1

#;> (* n (factorial (- n 1))))))

#;> (factorial 7)

5040

Page 31: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

31

PROLOG – Everything is a

relation!Pets to owners

owns(mary,bo).

owns(ann,kitty).

owns(bob,riley).

owns(susy,charlie).

?-owns(mary,bo)

yes

?-owns(bo,mary)

no

?-owns(susy,bo)

no

States

facts

Asks

questions

Page 32: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

32

Prolog

?-owns(ann, Cat).

Cat = kitty

?-owns(Name,charlie).

Name = susy

Upper case is

variable;

lower case

is constant

Page 33: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

33

READ FOR NEXT TIME: 9.4

Functionality of High-Level

Languages

Page 34: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

34

Prolog – another example(not in text)

Page 35: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

35

Prolog – another example(not in text)

Page 36: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

36

Prolog – another example(not in text)

Page 37: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

37

Prolog – another example(not in text)

Page 38: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

38

Prolog – another example(not in text)

?-ancestor(tom, jim).

yes

?-owns(tom,james).

no

Page 39: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

39

9.4 Functionality of

Procedural Languages

Variables

Input/Output

Control Structures

• loops

• decisions

Subprograms (a.k.a. functions)

Page 40: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

40

9.5 Functionality of OO

LanguagesEncapsulation

A language feature that enforces information hiding

Classes

Different meanings in different places (See next slide)

Inheritance

A property that allows a class to inherit the data and actions of another class

Polymorphism

A ability to handle the ambiguity of duplicate names

Page 41: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

41

Class Definition

A class encapsulates both data and actions

public class Person // Name the class

// Declare Class variables

Name name

String address

String telephone

String email

Page 42: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

42

Class Definition

// Declare Class Methods

Initialize() // Code for Initialize

public Print() // Code for Print

public Name GetName()

RETURN Name

public String GetAddress()

RETURN address

public String GetEmail()

RETURN email

public String GetTelephone()

RETURN telephone

Page 43: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

43

Class Definition

Figure 9.4 Class person

Page 44: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

44

Class Definition

Name aName = new Name()

aName.Initialize("Frank", "Jones")

Person aPerson = new Person()

aPerson.Initialize(aName, address, telephone, email)

aPerson.Print()

Write "Name: ", aPerson.GetName().Print()

Write "Address: ", aPerson.GetAddress()

Write " Telephone: ", aPerson.GetTelephone()

Write " Email: ", a Person.GetEmail()

Page 45: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

45

Class Definition

To get an object of a class, we must ask that one be

created (instantiated)

The new operator does this for us

Name aName = new Name()

Gets us a new Name object and stores its

address into aName

Person aPerson = new Person()

Gets a new Person object and stores its address

in aPerson.

Page 46: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

46

Inheritance

A construct that fosters reuse by allowing an application to take an already-tested class and derive a class from it that inherits the properties the application needs

Polymorphism

The ability of a language to have duplicate method names in an inheritance hierarchy and to apply the method that is appropriate for the object to which the method is applied

Page 47: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

47

Example of

inheritance and

polymorphism

Polymorphism:The Print()

methods are now

different, but we’re

still calling them by

the same name!

Student

GPA

Print()

Faculty

Rank

Print()

Page 48: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

48

Inheritance and Polymorphism

Inheritance and polymorphism work together

How?

They combine to allow the programmer to build

useful hierarchies of classes that can be put into a

library to be reused in different applications

Page 49: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

49

Ethical Issues

Gambling on the Internet

Have you ever visited an Internet gambling

site?

Should Internet gambling be outlawed?

Should Internet gambling be legalized and

regulated?

Should Internet gambling be taxed?

Page 50: Object-Oriented Design and High-Level Programming … · Chapter 9 Object-Oriented Design and High-Level ... essential ingredients of an object-oriented language ... –Based on principles

50

Who am I?

I am best known

for structured

programming.

Can you define

it?

I am also known

for my wit. Can

you recall some

of my witty

sayings?