lecture 11 abstraction
play

Lecture 11: Abstraction I ntro. to Programming, lecture 11: - PDF document

Chair of Softw are Engineering Einfhrung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 11: Abstraction I ntro. to Programming, lecture 11: Abstraction 2 Topics for today Abstraction, especially


  1. Chair of Softw are Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 11: Abstraction I ntro. to Programming, lecture 11: Abstraction 2 Topics for today Abstraction, especially functional abstraction The notion of routine The final word on features: all feature categories The Uniform Access principle Abstraction and client privileges Information hiding I ntro. to Programming, lecture 11: Abstraction 3

  2. Routine: algorithm abstraction To abstract is to capture the essence behind the details and the specifics Implies giving a name to the result In programming: � Data abstraction: class � Algorithm (operational) abstraction: routine I ntro. to Programming, lecture 11: Abstraction 4 A routine is one of the two kinds of feature... ... the other is attribute We have encountered lots of routines already, without the name. I ntro. to Programming, lecture 11: Abstraction 5 A routine r ( arg : TYPE ; ...) is -- Header comment require ... Precondition (boolean expression) ... do ... Body (instructions) ... ensure Postcondition (boolean expression) end I ntro. to Programming, lecture 11: Abstraction 6

  3. Uses of routines Bottom-up: capture existing algorithm, possibly for reuse Top-down: placeholder routines — attractive alternative to pseudocode. build_a_line is create_fancy_line is -- Build imaginary line -- Create line and fill stations do do Paris . display -- To be completed Metro . highlight -- BM, 30 Oct 07 end create_fancy_line end I ntro. to Programming, lecture 11: Abstraction 7 Two kinds of routine Procedure: doesn’t return a result � Yields a command � Calls are instructions Function: returns a result f ( arg : TYPE ; ...): RESULT_TYPE is ... (The rest as before) ... � Yields a query � Calls are expressions I ntro. to Programming, lecture 11: Abstraction 8 Features: the full story A class is characterized by its features Each feature is an operation on the corresponding objects: query or command Features are grouped into categories for readability Class clauses: � Indexing � Inheritance � Creation � Feature (any number) � Invariant Anatomy of a class: Demo I ntro. to Programming, lecture 11: Abstraction 9

  4. Features: the full story Client view Internal view (specification) (implementation) Command Procedure Routine No result Computation Feature Feature Feature Feature Memory Returns result Function Computation Query Memory Attribute I ntro. to Programming, lecture 11: Abstraction 10 Uniform access principle It doesn‘t matter to the client whether you look up or compute A call such as your_account . balance could use an attribute or a function I ntro. to Programming, lecture 11: Abstraction 11 Uniform Access: an example balance = list_of_deposits . total – list_of_withdrawals . total list_of_deposits (A1) list_of_withdrawals balance list_of_deposits (A2) list_of_withdrawals I ntro. to Programming, lecture 11: Abstraction 12

  5. An object has an interface set x y set_x set_y I ntro. to Programming, lecture 11: Abstraction 13 An object has an implementation set x y set_x set_y I ntro. to Programming, lecture 11: Abstraction 14 Information hiding set x y set_x set_y I ntro. to Programming, lecture 11: Abstraction 15

  6. Uniform Access Principle Expressed more technically: Features should be accessible to clients the same way whether implemented by storage or by computation I ntro. to Programming, lecture 11: Abstraction 16 Uniform Access: an example balance = list_of_deposits . total – list_of_withdrawals . total list_of_deposits (A1) list_of_withdrawals balance list_of_deposits (A2) list_of_withdrawals I ntro. to Programming, lecture 11: Abstraction 17 Uniform Access Principle Features should be accessible to clients the same way whether implemented by storage or by computation I ntro. to Programming, lecture 11: Abstraction 18

  7. What clients may do class METRO_STATION feature x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITION -- Upper-left position of bounding square adjust_positions is -- Set positions of bounding square do upper_left . set ( x – size/2, y + size/2 ) ... end end I ntro. to Programming, lecture 11: Abstraction 19 What clients may not do class METRO_STATION feature x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITION -- Upper-left position of bounding square NOT PERMITTED! adjust_positions is -- Set positions of bounding square do upper_left . x := 3 ... end end I ntro. to Programming, lecture 11: Abstraction 20 Use procedures: upper_left . set (3, upper_left . y ) upper_left . set_x (3 ) upper_left . move (3, h ) I ntro. to Programming, lecture 11: Abstraction 21

  8. Possible client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with att a : A C A do with ? a att a : A The attribute may be: Read, Read-only Secret restricted write Full write Modify through Example: modify “set_...” x with move in procedure POINT I ntro. to Programming, lecture 11: Abstraction 22 Possible client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with C A a : A a : A att do with ? a att The attribute may be: Read, Secret Read-only Full write restricted write Modify through Modify through a some_procedure a set_att ( v ) a att a att permitted in C (for access) invalid I ntro. to Programming, lecture 11: Abstraction 23 Abstraction and client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with att a : A C A do with ? a att a : A Read access if attribute is exported � a . att is an expression. � An assignment a . att := v would be syntactically illegal! (It would assign to an expression, like x + y := v .) I ntro. to Programming, lecture 11: Abstraction 24

  9. Applying abstraction principles 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 to u . do temperature := u ensure temperature_set: temperature = u end Client will use e.g. x . set_temperature (21.5). I ntro. to Programming, lecture 11: Abstraction 25 Other uses of a setter procedure set_temperature ( u : REAL ) is -- Set temperature value to u . require not_under_minimum: u >= -273 not_above_maximum: u <= 2000 do temperature := u update_database ensure temperature_set: temperature = u end I ntro. to Programming, lecture 11: Abstraction 26 Having it both ways Make it possible to call a setter procedure temperature: REAL assign set_temperature Then the syntax x . temperature := 21.5 is accepted as a shorthand for x . set_temperature (21.5) Retains contracts etc. I ntro. to Programming, lecture 11: Abstraction 27

  10. Information hiding class Status of calls in a client with a1 : A: A feature � a 1 . f , a1 . g : valid in any client f ... g ... feature { NONE } � a1 . h : invalid everywhere (including in A ’s own text!) h, i ... feature { B , C } � a1 . j : valid only in B , C and their descendants j, k, l ... (not valid in A !) feature { A , B , C } � a1 . m : valid in B , C and their descendants, m, n… end as well as in A and its descendants I ntro. to Programming, lecture 11: Abstraction 28 An example of selective export 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 LINKABLE cells. count 3 first_element active Haupt- Haldenegg Central bahnhof item right item right item right I ntro. to Programming, lecture 11: Abstraction 29 Exporting selectively These features are selectively exported to LINKED_LIST and its class descendants (and no other classes) LINKABLE [ G ] feature { LINKED_LIST } put_right (...) is do ... end right : G is do ... end ... end I ntro. to Programming, lecture 11: Abstraction 30

  11. 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 } h is ... do ... end feature f is do ...; h ; ... end end I ntro. to Programming, lecture 11: Abstraction 31 What we have seen I ntro. to Programming, lecture 11: Abstraction 32 What we have seen � Routines, procedures, functions � The full categorization of features � More on information hiding � Uniform access � Selective exports � Feature categories � Setters and getters � Eiffel: assigner commands I ntro. to Programming, lecture 11: Abstraction 33

  12. End of lecture 11 I ntro. to Programming, lecture 11: Abstraction 34

Recommend


More recommend