1
play

1 Underlying class (from business model) 4 class EDIT_CONTROLLER - PDF document

1 Introduction to Programming Bertrand Meyer Lecture 20 An example: undo-redo Last revised 20 January 2006 Chair of Softw are Engineering I ntroduction to Programming Lecture 20 The problem 2 Enabling users of an interactive system


  1. 1 Introduction to Programming Bertrand Meyer Lecture 20 An example: undo-redo Last revised 20 January 2006 Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 The problem 2 � Enabling users of an interactive system to cancel the effect of the last command � Often implemented as “Control-Z” � Should support multi-level undo-redo, with no limitation other than a possible maximum set by the user � A good review of O-O techniques Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Working example: text editor 3 Notion of “current line”. Assume commands such as: � Insert line after current position � Insert line before current position � Delete current line � Replace current line � Swap current line with next if any � ... This is a line-oriented view for simplicity, but the discussion applies to more sophisticated views Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 1

  2. Underlying class (from “business model”) 4 class EDIT_CONTROLLER feature text : LINKED_LIST [ STRING ] remove is -- Remove line at current position. require not off do text . remove end put_right ( line : STRING ) is -- Insert line after current position. require not after do text . put_right ( line ) end ... end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Key step in devising a software architecture 5 Finding the right abstractions (Interesting object types) Here: � The notion of “command” Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Keeping the history of the session 6 The history list: Insert Insert Remove Insert Swap Oldest Most recent history : LINKED_LIST [ COMMAND ] Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 2

  3. What’s a “command” object? 7 A command object (instance of the class COMMAND ) includes information about one execution of a command by the user, sufficient to: � Execute the command � Cancel the command if requested later For example, in a delete command (as implemented by remove ), we need: • The position of the line being deleted • The content of that line! Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 A general notion of command 8 deferred class COMMAND feature execute is -- Carry out one execution of this command. deferred end undo is -- Cancel an earlier execution of this command. deferred end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 A general notion of command 9 deferred class COMMAND feature done : BOOLEAN is -- Has this command been executed? execute is -- Carry out one execution of this command. deferred ensure done: done end undo is -- Cancel an earlier execution of this command. require already: done deferred end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 3

  4. Command class hierarchy 10 execute * ∗ COMMAND ∗ deferred undo * effective + + + … DELETION INSERTION execute + execute + undo + undo + line index index ... ... Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 A command class (sketch, no contracts) 11 class DELETION inherit COMMAND feature controller : EDIT_CONTROLLER -- Access to business model line : STRING -- The line being deleted index : INTEGER -- Position of line being deleted execute is -- Remove current line and remember it. do line := controller . item ; index := controller . index controller . remove ; done := True end undo is -- Re-insert previously removed line. do controller . go_ith ( index) controller . put_left ( line ) end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Underlying class (from “business model”) 12 class EDIT_CONTROLLER feature text : LINKED_LIST [ STRING ] remove is -- Remove line at current position. require not off do text . remove end put_right ( line : STRING ) is -- Insert line after current position. require not after do text . put_right (line) end ... also item, index, go_ith, put_left ... end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 4

  5. Executing a user command 13 The history list: Insert Insert Remove Insert Swap Oldest Most recent history : LINKED_LIST [ COMMAND ] Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Executing a user command 14 decode_user_request if “Request is normal command” then -- “Create command object c corresponding to user request” history . extend ( c ) c . execute Insert Insert Remove Insert elseif “Request is UNDO” then if not history . before then history . item . undo item history . back -- We ignore excessive requests end elseif “Request is REDO” then if not history . is_last then history . forth history . item . undo -- We ignore excessive requests end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Conditional creation (1) 15 A a1 : A if condition_1 then -- “Create a1 as an instance of B” … elseif condition_2 then -- “Create a1 as an instance of C” B C ... etc... a1 : A ; b1 : B ; c1 : C ; d1 : D ; ... … if condition_1 then create b1 . make (...) a1 := b1 D elseif condition_2 then create c1 . make (...) a1 := c1 ... etc... Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 5

  6. Conditional creation (2) 16 A a1 : A if condition_1 then -- “Create a1 as an instance of B” … elseif condition_2 then -- “Create a1 as an instance of C” B C ... etc... a1 : A … if condition_1 then create { B } a1 . make (...) elseif condition_2 then D create { C } a1 . make (...) ... etc... Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Executing a user command 17 decode_user_request if “Request is normal command” then -- “Create command object c corresponding to user request” history . extend ( c ) c . execute elseif “Request is UNDO” then Insert Insert Remove Insert if not history . before then history . item . undo item history . back -- Ignore excessive requests end elseif “Request is REDO” then if not history . is_last then history . forth history . item . undo -- Ignore excessive requests end end Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Creating command objects (1) 18 c : COMMAND ... decode_user_request if “Request is delete ” then create { DELETION } c elseif “Request is insert ” then create { INSERTION } c ... etc... Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 6

  7. Command class hierarchy 19 * execute * * deferred COMMAND undo * + effective + + … DELETION INSERTION execute + execute + undo + undo + line index index ... ... Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 Creating command objects (2) 20 � Give each command type a number (or other key) � Initially, fill in a table (e.g. an array), with one instance of each command type. ... ... Swap Insertion � To get a new command object: Deletion “Determine command_type ” c := clone ( COMMAND_TABLE . item ( command_type )) Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 The undo-redo pattern 21 � Has been extensively used (e.g. in Eiffel tools) � Fairly easy to implement � Details must be handled carefully (e.g. some commands may not be undoable) � Elegant use of O-O techniques � Disadvantage: explosion of small classes � In Java, you can use “inner” classes. Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 7

  8. Using agents 22 For each user command, have two routines: � The routine to do it � The routine to undo it! Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 The history list in the undo-redo pattern 23 history : LINKED_LIST [ COMMAND ] Insert Insert Remove Insert Swap Oldest Most recent Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 The history list using agents 24 The history list simply becomes a list of agents pairs: history : LINKED_LIST [ TUPLE [ PROCEDURE [ ANY, TUPLE ], PROCEDURE [ ANY, TUPLE ]] Insert Insert Remove Insert Swap De-insert De-insert Re-insert De-insert Swap Basic scheme remains the same, but no need for command objects any more; the history list simply contains agents. Chair of Softw are Engineering I ntroduction to Programming – Lecture 20 8

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