einf hrung in die programmierung einf hrung in die
play

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


  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

  2. Topics for today Abstraction, especially functional abstraction b ll f l b 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

  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

  4. A routine is one of the two kinds of feature... ... the other is attribute h h b We have encountered lots of routines already, without the name. 4

  5. A routine r ( arg : TYPE ; ...) is ( TYPE ) i -- Header comment require require Precondition (boolean expression) do Body (instructions) ensure Postcondition (boolean expression) end 5

  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 pseudocode. d d build_a_line is build a line is create_fancy_line is create fancy line is -- Build imaginary line. -- Create line and fill do -- stations. Paris.display p y do Metro . highlight -- To be completed -- BM, 26 Oct 08 create_fancy_line end end 6

  7. Two kinds of routine Procedure: doesn’t return a result d d ’ l � Yields a command � Calls are instructions C ll i t ti 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

  8. Features: the full story A class is characterized by its features l h d b f 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 Demo 8

  9. Features: the full story Client view Internal view (specification) (implementation) Command Command Procedure Routine Routine No result No result Computation Feature Feature Feature Feature Memory Memory Returns result Function Computation Computation Query Memory Attribute 9

  10. Uniform access principle It doesn‘t matter to the client whether you look up or compute h th l k t A call such as your_account . balance o o nt b l n e could use an attribute or a function could use an attribute or a function 10

  11. Uniform Access: an example b l balance = list_of_deposits . total – list_of_withdrawals . total l f d l l f hd l l list_of_deposits ( (A1) ) list_of_withdrawals balance list_of_deposits (A2) list_of_withdrawals 11

  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

  13. An object has an interface set x y set x set_x set_y 13

  14. An object has an im plem entation set first x y set x set_x count set_y 14

  15. Information hiding set x y set x set_x set_y 15

  16. What clients may do class METRO STATION feature class METRO_STATION feature x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITION l ft 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

  17. What clients may not do class METRO STATION feature class METRO_STATION feature x, x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square upper_left: POSITION l ft POSITION -- Upper-left position of bounding square-- Upperposition of bounding square adjust_positions is j _p ---- Set positions of bounding square do upper_left . x := 3 ... end NOT PERMITTED! end 17

  18. Use procedures: upper_left . set (3, upper_left . y ) upper_left . set_x (3 ) upper_left . move (3, h ) 18

  19. Possible client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with att att a : A C A do with ? a att a : A The attribute may be: Read, Read-only y Secret Secret restricted write restricted write Full write Full write Modify through Modify through Example: modify x Example: modify x “set_...” procedure with move in POINT 19

  20. Possible client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with C C A A a : A a : A att do with ? a att The attribute may be: Read, Read-only y Secret Secret restricted write restricted write Full write Full write Modify through Modify through Modify through Modify through a some_procedure a set_att ( v ) a a att att permitted in C (for access) permitted in C (for access) a a att att invalid 20

  21. Abstraction and client privileges If class A has an attribute att : SOME_TYPE , what may a client class C with att att a : A C A do with ? a att a : A 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

  22. Applying abstraction principles B Beyond read access: full or restricted write, through d d f ll t i t d it th h exported procedures. Full write privileges: set_attribute procedure, e.g. set_temperature ( u : REAL ) is -- Set temperature value to u . Set temperature value t u do temperature := u ensure temperature_set: temperature = u end Client will use e.g. x . set_temperature (21.5). 22

  23. Other uses of a setter procedure set_temperature ( u : REAL ) is -- Set temperature value to u . p require not_under_minimum: u >= -273 not_above_maximum: u <= 2000 b i 2000 do temperature := u temperature : u update_database ensure temperature_set: temperature = u end 23

  24. Having it both ways Make it possible to call a setter procedure k bl ll d 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

  25. Information hiding class Status of calls in a client with a1 : A: A feature � a 1 . f , a1 . g : valid in any client 1 f 1 lid i li t � f ... g ... � a1 . h : invalid everywhere feature { NONE } feature { NONE } (including in A ’s own text!) h, i ... � a1 . j : valid only in B , C and their � a1 j : valid only in B C and their f feature { B , C } { } descendants j, k, l ... (not valid in A !) feature { A , B , C } � a1 . m : valid in B , C and their descendants, m, n… as well as in A and its descendants end d 25

  26. An example of selective export LINKABLE exports its features to LINKED_LIST E f E � 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. count 3 first_element active Haupt Haupt- Haldenegg Central bahnhof right item right item right item 26

  27. Exporting selectively Th These features are selectively f t l ti l exported to LINKED_LIST and its class descendants (and no other classes) LINKABLE [ G ] LINKABLE [ G ] feature { LINKED_LIST } put_right (...) is do ... end right : G is do ... end ... end 27

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend