40
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

  • Upload
    geona

  • View
    15

  • Download
    0

Embed Size (px)

DESCRIPTION

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer. Lecture 2: Dealing with Objects I. Our first program!. Display a map of Paris Spotlight position of Louvre museum Highlight line 8 of the metro Animate a predefined route. A class text. class - PowerPoint PPT Presentation

Citation preview

Page 1: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand Meyer

Lecture 2: Dealing with Objects I

Page 2: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

2

Our first program!Display a map of Paris

Spotlight position of Louvre museum

Highlight line 8 of the metro

Animate a predefined route

Page 3: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

3

classPREVIEW

inheritTOURISM

featureexplore

-- Show city info and route.

do-- “To be filled in (by

you!)”end

end

Class: a software machine

A class text

The class name

Page 4: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

4

Traffic library conventionTraffic classes have names of the form

TRAFFIC_ACTUAL_CLASS_NAME

In these slides and in the book, for brevity, I omit the TRAFFIC_ part of the name

But you will need it to find the classes in the software

Page 5: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

5

Another conventionFor long names, use underscores “_”

TRAFFIC_STATIONStation_Paradeplatz -- or: Station_Parade_Platz

We do not use “CamelCase”:

AShortButHardToDeCipherName

but underscores (sometimes called “Pascal_case”):

A_significantly_longer_but_still_perfectly_clear_name

Page 6: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

6

classPREVIEW

inheritTOURISM

featureexplore

-- Show city info and route.

do-- “To be filled in (by

you!)”end

end

Software machine Extend existing

classOperations

Featurename

Comment

Keywords have a special role: class, inherit, feature, do, end.

Feature declaration

Pseudocode

A class text

Page 7: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

7

Magic?Class TOURISM is part of the supporting software It helps you learn by using predefined facilities (“magic”)

Little by little pieces of the magic will be removed

At the end, the magic will be gone

Page 8: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

8

classPREVIEW

inheritTOURISM

feature explore

-- Show city info and route.

doParisdisplayLouvrespotlightLine8highlightRoute1animate

endend

Filling in the feature body

Michela Pedroni
Start EiffelStudio and show what happens if this body is filled in...
Page 9: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

9

Program formatting

Between adjacent elements: break: one or more spaces,

“tabs”, “carriage returns”

All kinds of break are equivalent

Typographical variations (boldface, italics, colors) do not affect the effect (semantics) of programs

class PREVIEW inherit TOURISMfeature explore -- Show city info -- and route. do Parisdisplay Louvrespotlight Line8highlight Route1animate endend

Breaks

Breaks

Page 10: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

10

Style rule

For indentation, use tabs, not spacesUse this property to highlight the structure of the program, particularly through indentation

class PREVIEW inherit TOURISMfeature explore -- Show city info -- and route. do Parisdisplay Louvrespotlight Line8highlight Route1animate endend

Tabs

Page 11: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

11

Feature callThe fundamental mechanism of program execution: apply a “feature” to an “object”Basic form: your_object.your_feature

classPREVIEW

inheritTOURISM

featureexplore

-- Show city info-- and route.

do

Parisdisplay

Louvrespotlight Line8highlight Route1 animate

endend

Object (target of the call)

Feature of the call

Page 12: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

12

Predefined objectsParis, Louvre, Line8, and Route1 are names of predefined objects

The objects are defined in class TOURISM from which PREVIEW inherits

display, spotlight, highlight and animate are features, applicable to these objects

Page 13: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

13

Class name: all upper-case

Period in feature call: no space before or after

Names of predefined objects: start with upper-case letters

New names (for objects you define) start with lower-case letters

classPREVIEW

inheritTOURISM

featureexplore

-- Show city info-- and route.

do

ParisdisplayLouvrespotlightLine8highlight

Route1animateend

end

More style rules

Page 14: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

14

Object technologyWe work with objects

Our style of programming: Object-Oriented programming Abbreviation: O-O

More generally, “Object Technology”: includes O-O databases, O-O analysis, O-O design...

Software execution is made of operations on objects — feature calls

your_object.your_feature

Page 15: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

15

A distinct mode of expression

Parisdisplay

next_messagesendcomputershut_downtelephonering

Every operation applies to an object(the target of the call)

Page 16: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

16

What’s an object?Software notion: machine known through operations applicable to it.Three kinds of object:

“Physical objects”: reflect material objects of the modeled world or system

Examples: the Louvre, Paris, a metro car..“Abstract objects”: describe abstract notions from the modeled world or system

Examples: a line, a route...“Software objects”: represent pure software notions

Examples: “data structures” such as arrays or lists

A key attraction of object technology is its modeling power: connect software objects to objects of the problem domain (“model”)You should not, however, confuse themIn this course, “object” by default means software object

Page 17: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

17

Two views of objects

Two viewpoints: 1. An object has data, stored in memory. 2. An object is a machine offering operations

(features)

The connection: The operations that the machine provides (2) access

and modify the object’s data (1).

“Bürkliplatz”

“Bucheggplatz”

255

Start pointTram

numberNumber of stops

Endpoint

Page 18: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

18

Features: commands and queries

Feature: an operation available on a certain class of objects

Three kinds: Command Query

Creation procedure (seen later)

Page 19: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

19

QueriesGoal: obtain properties of objects

Should not modify the object,or any other

Examples, for “route” objects:

What is the origin (first station) of Route1? What is the end point of Route1? How many stations does Route1 have? Which stations does Route1 traverse?

“Bürkliplatz”

“Bucheggplatz”

255

Page 20: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

20

CommandsGoal: produce a change on an object, or several objects

Examples, for “route” objects:

Animate Route1

Append (add at the end) a station to Route1

Prepend (add at the beginning) a station to Route1

Page 21: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

21

A command

Page 22: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

22

A query

Page 23: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

23

The command-query separation principle

Asking a question

should not change the answer

Page 24: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

24

An object is a machine

An executing program is a machineIt’s made of smaller machines: objects

During execution there may be many objects (e.g. millions)

Page 25: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

25

An object is a machine

A machine, hardware or software, is characterized by the operations (“features”) users may apply

prepend

animateappend

count stations

first last

Page 26: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

26

Two views of objects

Two viewpoints: 1. An object has data, stored in memory. 2. An object is a machine offering operations

(features: commands and queries)

The connection: The operations that the machine provides (2)

access and modify the object’s data (1).

“Bürkliplatz”

“Bucheggplatz”

255

Start pointTram

numberNumber of stops

Endpoint

Page 27: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

27

Objects: a definition

An object is a software machine allowing programs to access and modify a collection of data

Page 28: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

28

Defining and classifying features

A feature is an operation that programs may apply to certain classes of objects.

• A feature that accesses an object is a query

• A feature that may modify an object is a command

Page 29: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

29

Using queriesQueries are as important as commands

Queries don’t “do” anything, but yield a value, e.g. Route1origin yields the starting station of Route1

You may work with the return values of queries, e.g. highlight the starting station on the screen

Page 30: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

30

Features may have arguments

Task: Show starting point of Route1 on “console”

windowYou need:

Predefined object Console Feature show applicable to Console The object Route1 Feature origin returning starting point and

applicable to Route1The new feature call:

Consoleshow (Route1 origin)

Page 31: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

31

class PREVIEW inherit TOURISM

featureexplore

-- Show city info, a route, and route’s origin.

doParisdisplayLouvrespotlightLine8highlight

Route1animate

endend

Extending the feature body

Consoleshow (Route1origin )

Michela Pedroni
live demonstration
Page 32: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

32

Features with arguments

some_argument is a value that your_feature needs

Example: feature show must know what to show.

Same concept as function arguments in maths:cos (x)

Features may have several arguments:

xf (a, b, c, d ) -- Separated by commas

In well written O-O software, most have 0 or 1 argument

your_objectyour_feature (some_argument)

Page 33: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

33

A distinct mode of expression

Parisdisplay

next_messagesendcomputershut_downtelephonering

Every operation applies to an object

Page 34: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

34

A distinct mode of expression

Parisdisplay

next_messagesend_to (recipient )computershut_down_after (3 )telephonering_several (10, Loud )

Every operation applies to an object and may take arguments

Page 35: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

35

Scaling upOne of the toughest issues in learning software is to find solutions that work well both “in the small” and “in the large”.

That’s the goal for the techniques we teach in this course.

Page 36: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

36

prepend

animateappend

An object has an interface

count stations

first last

Page 37: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

37

prepend

animateappend

count

first

An object has an implementation

count stations

first last

Page 38: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

38

Information hiding

prepend

animateappend

count stations

first last

Page 39: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

39

The information hiding principle

The designer of every modulemust specify which properties

are accessible to clients (public)and which are internal (secret)

The programming languagemust ensure that clients

can only use public properties

Page 40: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

40

What we have seen so farBasic concepts and constructs of object technology:

Classes (a first view) Basic program text structure Objects Features Feature call Feature arguments

Methodological principles: Command-query separation Information hiding