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

1
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

I ntroduction to Programming – Lecture 20

1 Chair of Softw are Engineering

Introduction to Programming Bertrand Meyer

Last revised 20 January 2006

Lecture 20 An example: undo-redo

I ntroduction to Programming – Lecture 20

2 Chair of Softw are Engineering

The problem

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

I ntroduction to Programming – Lecture 20

3 Chair of Softw are Engineering

Working example: text editor

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

slide-2
SLIDE 2

2

I ntroduction to Programming – Lecture 20

4 Chair of Softw are Engineering

Underlying class (from “business model”)

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

I ntroduction to Programming – Lecture 20

5 Chair of Softw are Engineering

Key step in devising a software architecture Here: The notion of “command”

Finding the right abstractions

(Interesting object types)

I ntroduction to Programming – Lecture 20

6 Chair of Softw are Engineering

Keeping the history of the session

The history list:

Insert Insert Remove Swap Insert Oldest Most recent

history : LINKED_LIST [COMMAND]

slide-3
SLIDE 3

3

I ntroduction to Programming – Lecture 20

7 Chair of Softw are Engineering

What’s a “command” object?

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!

I ntroduction to Programming – Lecture 20

8 Chair of Softw are Engineering

A general notion of command

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

I ntroduction to Programming – Lecture 20

9 Chair of Softw are Engineering

A general notion of command

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

slide-4
SLIDE 4

4

I ntroduction to Programming – Lecture 20

10 Chair of Softw are Engineering

Command class hierarchy

COMMAND

DELETION INSERTION

execute* undo*

execute+ undo+ line index ... execute+ undo+ index ...

+ +

+

∗ deferred

effective

I ntroduction to Programming – Lecture 20

11 Chair of Softw are Engineering

A command class (sketch, no contracts)

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

I ntroduction to Programming – Lecture 20

12 Chair of Softw are Engineering

Underlying class (from “business model”)

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

slide-5
SLIDE 5

5

I ntroduction to Programming – Lecture 20

13 Chair of Softw are Engineering

Executing a user command

The history list:

Insert Insert Remove Swap Insert Oldest Most recent

history : LINKED_LIST [COMMAND]

I ntroduction to Programming – Lecture 20

14 Chair of Softw are Engineering

Executing a user command

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 if not history.before then history.item.undo 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

Insert Insert Remove Insert

item

I ntroduction to Programming – Lecture 20

15 Chair of Softw are Engineering

Conditional creation (1)

A

B C D

a1: A if condition_1 then

  • - “Create a1 as an instance of B”

elseif condition_2 then

  • - “Create a1 as an instance of C”

... etc... a1 : A; b1 : B ; c1 : C ; d1 : D ; ... if condition_1 then create b1.make (...) a1 := b1 elseif condition_2 then create c1.make (...) a1 := c1 ... etc...

slide-6
SLIDE 6

6

I ntroduction to Programming – Lecture 20

16 Chair of Softw are Engineering

Conditional creation (2)

A

B C D

a1: A if condition_1 then

  • - “Create a1 as an instance of B”

elseif condition_2 then

  • - “Create a1 as an instance of C”

... etc... a1: A if condition_1 then create {B } a1.make (...) elseif condition_2 then create {C } a1.make (...) ... etc...

I ntroduction to Programming – Lecture 20

17 Chair of Softw are Engineering

Executing a user command

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 if not history.before then history.item.undo 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

Insert Insert Remove Insert

item

I ntroduction to Programming – Lecture 20

18 Chair of Softw are Engineering

Creating command objects (1)

c : COMMAND ... decode_user_request if “Request is delete” then create {DELETION } c elseif “Request is insert” then create {INSERTION } c ... etc...

slide-7
SLIDE 7

7

I ntroduction to Programming – Lecture 20

19 Chair of Softw are Engineering

Command class hierarchy

COMMAND

*

DELETION INSERTION

execute* undo*

execute+ undo+ line index ... execute+ undo+ index ...

+ +

+

* deferred

effective

I ntroduction to Programming – Lecture 20

20 Chair of Softw are Engineering

Creating command objects (2)

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. To get a new command object:

“Determine command_type” c := clone (COMMAND_TABLE.item (command_type))

Deletion Insertion Swap ... ...

I ntroduction to Programming – Lecture 20

21 Chair of Softw are Engineering

The undo-redo pattern

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.

slide-8
SLIDE 8

8

I ntroduction to Programming – Lecture 20

22 Chair of Softw are Engineering

Using agents

For each user command, have two routines: The routine to do it The routine to undo it!

I ntroduction to Programming – Lecture 20

23 Chair of Softw are Engineering

The history list in the undo-redo pattern

Insert Insert Remove Swap Insert Oldest Most recent

history: LINKED_LIST [COMMAND]

I ntroduction to Programming – Lecture 20

24 Chair of Softw are Engineering

The history list using agents

The history list simply becomes a list of agents pairs: history : LINKED_LIST [TUPLE [PROCEDURE [ANY, TUPLE ], PROCEDURE [ANY, TUPLE ]] Basic scheme remains the same, but no need for command objects any more; the history list simply contains agents.

Insert Insert Remove Swap Insert

De-insert De-insert Re-insert Swap De-insert

slide-9
SLIDE 9

9

I ntroduction to Programming – Lecture 20

25 Chair of Softw are Engineering

Executing a user command (before)

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 if not history.before then history.item.undo 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

Insert Insert Remove Insert

item

I ntroduction to Programming – Lecture 20

26 Chair of Softw are Engineering

Executing a user command (now)

“Decode user_request giving two agents do_it and undo_it” if “Request is normal command” then history.extend ([do_it, undo_it]) do_it.call ([]) elseif “Request is UNDO” then if not history.before then history.item.item (2).call ([]) history.back end elseif “Request is REDO” then if not history.is_last then history.forth history.item.item (1).call ([]) end end

Insert Insert Remove Swap De- insert De- insert Re- insert Swap

I ntroduction to Programming – Lecture 20

27 Chair of Softw are Engineering

Lessons

Generality of inheritance and dynamic binding Implementation can be turned into a library component Agents nicely complement the basic O-O mechanisms

slide-10
SLIDE 10

10

I ntroduction to Programming – Lecture 20

28 Chair of Softw are Engineering

End of lecture 20