Einfhrung in die Programmierung Einfhrung in die Programmierung - - PowerPoint PPT Presentation

einf hrung in die programmierung einf hrung in die
SMART_READER_LITE
LIVE PREVIEW

Einfhrung in die Programmierung Einfhrung in die Programmierung - - PowerPoint PPT Presentation

Chair of Softw are Engineering Einfhrung in die Programmierung Einfhrung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction Topics for today Abstraction, especially functional abstraction b


slide-1
SLIDE 1

Chair of Softw are Engineering

Einführung in die Programmierung Einführung in die Programmierung Introduction to Programming

  • Prof. Dr. Bertrand Meyer

Lecture 9: Abstraction

slide-2
SLIDE 2

Topics for today

b ll f l b Abstraction, especially functional abstraction The notion of routine The notion of routine The final word on features: all feature categories The final word on features: all feature categories The Uniform Access principle p p Abstraction and client privileges Information hiding

2

slide-3
SLIDE 3

Routine: algorithm abstraction

To abstract is to capture the essence behind the details and the specifics the specifics Implies giving a name to the result In programming:

Data abstraction: class Algorithm (operational) abstraction: routine

3

slide-4
SLIDE 4

A routine is one of the two kinds of feature...

h h b ... the other is attribute We have encountered lots of routines already, without the name.

4

slide-5
SLIDE 5

A routine

( TYPE ) i r (arg : TYPE ; ...) is

  • - Header comment

require require Precondition (boolean expression) do Body (instructions) ensure Postcondition (boolean expression) end

5

slide-6
SLIDE 6

Uses of routines

Bottom-up: capture existing algorithm possibly for reuse Bottom-up: capture existing algorithm, possibly for reuse Top-down: placeholder routines — attractive alternative to d d

create fancy line is

pseudocode.

build a line is create_fancy_line is

  • - Create line and fill
  • - stations.

do build_a_line is

  • - Build imaginary line.

do Paris.display

  • - To be completed
  • - BM, 26 Oct 08

end p y Metro.highlight create_fancy_line end

6

slide-7
SLIDE 7

d d ’ l

Two kinds of routine

Procedure: doesn’t return a result

Yields a command C ll i t ti Calls are instructions

Function: returns a result f (arg : TYPE; ...): RESULT TYPE is f (arg TYPE; ...) RESULT_TYPE is ... (The rest as before) ...

Yields a query Calls are expressions

7

slide-8
SLIDE 8

Features: the full story

l h d b f A class is characterized by its features Each feature is an operation on the corresponding objects: query or command query or command Features are grouped into categories for readability Class clauses: Class clauses:

Indexing Inheritance Creation Feature (any number) Invariant

Anatomy of a class: Demo

8

Demo

slide-9
SLIDE 9

Features: the full story

Command

Client view (specification) Internal view (implementation) Procedure

Command

No result

Routine Feature Feature Feature

No result Memory Computation

Routine Feature

Function

Computation Returns result Memory

Query

Memory Computation

Attribute

9

slide-10
SLIDE 10

Uniform access principle

It doesn‘t matter to the client h th l k t whether you look up or compute

A call such as

  • nt b l n e

your_account. balance could use an attribute or a function could use an attribute or a function

10

slide-11
SLIDE 11

Uniform Access: an example

b l l f d l l f hd l l balance = list_of_deposits.total – list_of_withdrawals.total

list_of_deposits list_of_withdrawals

(A1)

balance

( )

list_of_deposits list_of_withdrawals

(A2)

11

slide-12
SLIDE 12

Uniform Access Principle

Expressed more technically: Features should be accessible to clients the same way whether implemented by storage or by computation whether implemented by storage or by computation

12

slide-13
SLIDE 13

An object has an interface

set set x x y set_y set_x

13

slide-14
SLIDE 14

An object has an im plem entation

first x y set set x count set_y set_x

14

slide-15
SLIDE 15

Information hiding

x y set set x set_y set_x

15

slide-16
SLIDE 16

class METRO STATION feature

What clients may do

class METRO_STATION feature x, y: REAL

  • - Coordinates of metro station

size: REAL

  • - Size of bounding square

l ft POSITION upper_left: POSITION

  • - Upper-left position of bounding square

adjust positions is adjust_positions is

  • - Set positions of bounding square

do upper_left . set (x – size/2, y + size/2) ... end end

16

slide-17
SLIDE 17

class METRO STATION feature

What clients may not do

class METRO_STATION feature x, x, y: REAL

  • - Coordinates of metro station

size: REAL

  • - Size of bounding square

l ft POSITION upper_left: POSITION

  • - Upper-left position of bounding square-- Upperposition
  • f bounding square

adjust_positions is j _p

  • --- Set positions of bounding square

do upper_left . x := 3 ... end end

NOT PERMITTED!

17

slide-18
SLIDE 18

Use procedures:

upper_left.set (3, upper_left.y ) upper_left.set_x (3 ) upper_left.move (3, h)

18

slide-19
SLIDE 19

Possible client privileges

If class A has an attribute att : SOME_TYPE, what may a client class C with

att

a : A do with ? a att C A a : A

att

Secret Read, restricted write Full write Read-only

The attribute may be:

Secret restricted write Full write y Example: modify x Modify through Example: modify x with move in POINT Modify through “set_...” procedure

19

slide-20
SLIDE 20

Possible client privileges

If class A has an attribute att : SOME_TYPE, what may a client class C with C A a : A do with ? a att C A a : A

att

Secret Read, restricted write Full write Read-only

The attribute may be:

Secret restricted write Full write y Modify through Modify through

a att a att permitted in C (for access)

a some_procedure Modify through a set_att (v) Modify through

20

a att invalid a att permitted in C (for access)

slide-21
SLIDE 21

Abstraction and client privileges

If class A has an attribute att : SOME_TYPE, what may a client class C with

att

a : A do with ? a att C A a: A

att

Read access if attribute is exported

a att is an expression a.att is an expression. An assignment a.att := v would be syntactically illegal!

(It would assign to an expression, like x + y := v.)

21

slide-22
SLIDE 22

Applying abstraction principles

B d d f ll t i t d it th h Beyond read access: full or restricted write, through exported procedures. Full write privileges: set_attribute procedure, e.g. set_temperature (u : REAL) is Set temperature value t u

  • - Set temperature value to u.

do temperature := u ensure temperature_set: temperature = u end Client will use e.g. x.set_temperature (21.5).

22

slide-23
SLIDE 23

Other uses of a setter procedure

set_temperature (u : REAL) is

  • - Set temperature value to u.

p require not_under_minimum: u >= -273 b i 2000 not_above_maximum: u <= 2000 do temperature := u temperature : u update_database ensure temperature_set: temperature = u end

23

slide-24
SLIDE 24

k bl ll d

Having it both ways

Make it possible to call a setter procedure temperature: REAL assign set temperature mp E L g _ mp Then the syntax x.temperature := 21.5 is accepted as a shorthand for x.set_temperature (21.5) Retains contracts etc.

24

slide-25
SLIDE 25

Information hiding

Status of calls in a client with a1: A:

class A feature

  • 1 f 1

lid i li t

f ... g ... feature {NONE}

a1.f, a1.g: valid in any client

a1.h: invalid everywhere

feature {NONE} h, i ... f { }

(including in A’s own text!) a1 j: valid only in B C and their

feature {B, C} j, k, l ...

a1.j: valid only in B, C and their descendants (not valid in A!)

feature {A, B, C} m, n… d

a1.m: valid in B, C and their descendants, as well as in A and its descendants

25

end

slide-26
SLIDE 26

An example of selective export

E f E LINKABLE exports its features to LINKED_LIST

Does not export them to the rest of the world Clients of LINKED LIST don’t need to know about Clients of LINKED_LIST don t need to know about

LINKABLE cells.

first_element count 3 Haupt active Haldenegg item right Central item right Haupt- bahnhof item right

26

slide-27
SLIDE 27

Th f t l ti l

Exporting selectively

class LINKABLE [G]

These features are selectively exported to LINKED_LIST and its descendants (and no other classes)

LINKABLE [G] feature {LINKED_LIST } put_right (...) is do ... end right: G is do ... end ... end

27

slide-28
SLIDE 28

Information hiding

Information hiding only applies to use by clients, using dot notation or infix notation, as with a1.f (Qualified calls). Unqualified calls (within class) not subject to information hiding: class A feature {NONE } class A feature {NONE } h is ... do ... end feature f is do ...; h ; ... end end

28

slide-29
SLIDE 29

What we have seen

The full categorization of features Routines procedures functions Routines, procedures, functions Uniform access Information hiding Selective exports Setters and getters Eiffel: assi ner c mmands Eiffel: assigner commands

29