1 Keeping the history of the session Whats a command object? 7 8 - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Keeping the history of the session Whats a command object? 7 8 - - PDF document

1 2 Lecture 23: Introduction to Programming An example: undo-redo Bertrand Meyer Last revised 13 January 2005 Chair of Softw are Engineering I ntroduction to Programming Lecture 23 Chair of Softw are Engineering I ntroduction to


slide-1
SLIDE 1

1

I ntroduction to Programming – Lecture 23 1 Chair of Softw are Engineering

Introduction to Programming Bertrand Meyer

Last revised 13 January 2005

I ntroduction to Programming – Lecture 23 2 Chair of Softw are Engineering

Lecture 23: An example: undo-redo

I ntroduction to Programming – Lecture 23 3 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 23 4 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

I ntroduction to Programming – Lecture 23 5 Chair of Softw are Engineering

Underlying class (from “business model”)

class EDIT_CONTROLLER feature text: LI NKED_LIST [ STRING] remove is

  • - Remove line at current position.

require not off do text.remove end put_right (line: STRI NG) is

  • - Insert line after current position.

require not after do text.put_right (line) end ... end

I ntroduction to Programming – Lecture 23 6 Chair of Softw are Engineering

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

Finding the right abstractions

(Interesting object types)

slide-2
SLIDE 2

2

I ntroduction to Programming – Lecture 23 7 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]

I ntroduction to Programming – Lecture 23 8 Chair of Softw are Engineering

What’s a “command” object?

A command object (instance of the class COMMAND) includes inform ation about one execution of a command by the user, sufficient to: Execute the command Cancel the com mand 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 23 9 Chair of Softw are Engineering

A general notion of command

deferred class COMMAND feature execute is

  • - Carry out one execution of this command.

undo is

  • - Cancel an earlier execution of this command.

end deferred ensure done: done end require already: done deferred end

I ntroduction to Programming – Lecture 23 10 Chair of Softw are Engineering

A general notion of command

deferred class COMMAND feature execute is

  • - Carry out one execution of this command.

undo is

  • - Cancel an earlier execution of this command.

end deferred ensure done: done end require already: done deferred end done: BOOLEAN is

  • - Has this command been executed?
I ntroduction to Programming – Lecture 23 11 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 23 12 Chair of Softw are Engineering

A command class (sketch, no contracts)

class DELETION inherit COMMAND feature controller: EDIT_CONTROLLER

  • - Access to business m odel

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

slide-3
SLIDE 3

3

I ntroduction to Programming – Lecture 23 13 Chair of Softw are Engineering

Underlying class (from “business model”)

class EDIT_CONTROLLER feature text: LI NKED_LIST [ STRING] remove is

  • - Remove line at current position.

require not off do text.remove end put_right (line: STRI NG) is

  • - Insert line after current position.

require not after do text.put_right (line) end ... Also item, index, go_ith, put_left ... end

I ntroduction to Programming – Lecture 23 14 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 23 15 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 23 16 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...

I ntroduction to Programming – Lecture 23 17 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 23 18 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

slide-4
SLIDE 4

4

I ntroduction to Programming – Lecture 23 19 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...

I ntroduction to Programming – Lecture 23 20 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 23 21 Chair of Softw are Engineering

Creating command objects (2)

Give each com mand 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 23 22 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, can use “inner” classes.

I ntroduction to Programming – Lecture 23 23 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 23 24 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]

slide-5
SLIDE 5

5

I ntroduction to Programming – Lecture 23 25 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

  • bjects any more; the history list simply contains agents.

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

I ntroduction to Programming – Lecture 23 26 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 I ntroduction to Programming – Lecture 23 27 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 23 28 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

I ntroduction to Programming – Lecture 23 29 Chair of Softw are Engineering

End of lecture 23