bertrand meyer lecture 6 more patterns visitor strategy
play

Bertrand Meyer Lecture 6: More patterns: Visitor, Strategy, Chain, - PDF document

Software Architecture Software Architecture Bertrand Meyer Lecture 6: More patterns: Visitor, Strategy, Chain, State, Command 1 Command 2 2 Command pattern - Intent Purpose Way to implement an undo-redo mechanism, e.g. in text y p g


  1. Software Architecture Software Architecture Bertrand Meyer Lecture 6: More patterns: Visitor, Strategy, Chain, State, Command 1

  2. Command 2 2

  3. Command pattern - Intent Purpose Way to implement an undo-redo mechanism, e.g. in text y p g editors. [OOSC, p 285-290] ”Way to encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable t l t d t d bl operations. ” [Gamma et al., p 233] Application example A li ti l EiffelStudio 3 3

  4. 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 pp , other than a possible maximum set by the user A good review of O-O techniques 4 4

  5. 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 S t li ith t if � ... This is a line-oriented view for simplicity, but the This is a line oriented view for simplicity but the discussion applies to more sophisticated views 5 5

  6. Underlying class (from “business model”) class EDIT_CONTROLLER feature text : LINKED_LIST [ STRING ] [ ] remove -- Remove line at current position. require not off do text.remove end d put_right ( line: STRING ) -- Insert line after current position. require not after do do text.put_right ( line ) end ... end 6 6

  7. Key step in devising a software architecture Finding the right abstractions Finding the right abstractions (Interesting object types) Here: The notion of “command” 7 7

  8. Keeping the history of the session The history list: Insert Insert Remove Insert Swap Oldest Most recent history : LINKED_LIST [ COMMAND ] history : LINKED LIST [ COMMAND ] 8 8

  9. What’s a “command” object? An instance of COMMAND includes information about one execution of a command by the user, sufficient to: execut on of a command by the user, suff c ent to � Execute the command � Cancel the command if requested later For example, in a delete command object, we need: • The position of the line being deleted • The content of that line 9 9

  10. General notion of command deferred class COMMAND feature done: BOOLEAN done: BOOLEAN -- Has this command been executed? execute -- Carry out one execution of this command. deferred ensure : done already: done end undo -- Cancel an earlier execution of this command. require i already: done deferred end end 10 10

  11. Command class hierarchy * execute * * undo * COMMAND deferred + effective + + + … DELETION INSERTION execute + execute + undo + undo + index index li line ... index ... 11 11

  12. A command class (sketch, no contracts) class DELETION inherit COMMAND feature controller : EDIT_CONTROLLER -- Access to business model A b i d l line : STRING -- Line being deleted index : INTEGER -- Position of line being deleted execute x ut -- Remove current line and remember it. do line := controller.item ; index := controller.index controller.remove ; done := True end undo -- Re-insert previously removed line. R i i l d li do controller.go_ith ( index ) controller.put_left ( line ) end end 12 12

  13. Executing a user command decode_user_request if “Request normal command” then if Request normal command then “Create command object c corresponding to user request” history.extend ( c ) c.execute elseif “Request UNDO” then if not history.before then -- Ignore excessive requests history.item.undo history.item.undo history.back end Remove Insert Insert Insert elseif “Request REDO” then if not history.is_last then -- Ignore excessive requests item history.forth history.item.execute history item execute end end 13 13

  14. Command pattern: overall architecture * history history commands commands APPLICATION COMMAND HISTORY execute* execute undo* can_undo , can_redo redo* undo , redo undo_all , redo_all extend extend + + COMMAND_2 COMMAND_1 execute+ execute+ undo+ undo+ redo+ d + redo+ d + 14 14

  15. The undo-redo pattern Has been extensively used Fairly easy to implement y y mp m Details must be handled carefully (e.g. some commands may not be undoable) Elegant use of O-O techniques Disadvantage: explosion of small classes g p In Java, can use “inner” classes. 15 15

  16. Using agents For each user command, have two routines: � The routine to do it � The routine to undo it! 16 16

  17. 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 ], PROCEDURE [ ANY, TUPLE ]] Basic scheme remains the same, but no need for command objects any more; the history list simply contains agents. th hi t li t i l t i t Insert Insert Remove Insert Swap De- De- Re-insert De- Swap insert insert insert 17 17

  18. Executing a user command (before) decode_user_request if “Request is normal command” then 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 -- Ignore excessive requests if not history before then Ignore excessive requests history . item . undo history . back Remove Insert Insert Insert end item elseif “Request is REDO” then if not history . is_last then -- Ignore excessive requests history . forth history . item . execute end end 18 18

  19. Executing a user command (now) “Decode user_request giving two agents do_it and undo_it ” if “Request is normal command” then q history . extend ([ do_it , undo_it ]) do_it . call ([]) Insert Insert Remove Swap De- De- Re- Swap elseif “Request is UNDO” then insert insert insert if not history . before then y history . item . item (2) . call ([]) history . back end elseif “Request is REDO” then if not history . is_last then if not history is last then history . forth history . item . item (1) . call ([]) end 19 end 19

  20. Command - Consequences Command decouples the object that invokes the operation from the one that knows how to perform it. Commands are first-class objects. They can be manipulated and extended like any other object. You can assemble commands into a composite command. It's easy to add new Commands, because you don't have to change existing classes. 20 20

  21. Command - Participants Command declares an interface for executing an operation. g p Concrete command � defines a binding between a Receiver object and an action. � implements Execute by invoking the corresponding operation(s) on Receiver. Client creates a ConcreteCommand object and sets its receiver. Invoker asks the command to carry out the request. asks the command to carry out the request. Receiver knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver. 21 21

  22. Some design patterns Behavioral Creational � Chain of Responsibility � Chain of Responsibility � Ab t � Abstract Factory t F t � Command (undo/redo) � Builder � Interpreter � Factory Method � Iterator � Prototype � Mediator � Singleton � Memento � Memento Structural � Observer � Adapter � State � Bridge � Strategy � Composite � Template Method � Decorator � Visitor � Visitor � Façade � Flyweight � Proxy 22

  23. Visitor 23 23

  24. Visitor - Intent “Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.” [Gamma et al., p 331] � Static class hierarchy � Need to perform traversal operations on corresponding data structures � Avoid changing the original class structure � Avoid changing the original class structure 24 24

  25. Visitor application example Set of classes to deal with an Eiffel or Java program (in EiffelStudio, Eclipse ...) , p ) Or: Set of classes to deal with XML documents ( XML_NODE, XML_DOCUMENT, XML_ELEMENT, XML_ATTRIBUTE, XML_CONTENT…) One parser (or several: keep comments or not…) M Many formatters: f tt � Pretty-print � Compress � Convert to different encoding � Generate documentation � G t d t ti � Refactor � … 25 25

  26. Inheritance hierarchy center * display * * FIGURE rotate* * * OPEN_ perimeter * CLOSED_ FIGURE FIGURE perimeter + perimeter + + SEGMENT SEGMENT POLYLINE POLYLINE + + POLYGON POLYGON ELLIPSE ... ... side1 RECTANGLE side2 TRIANGLE diagonal perimeter ++ perimeter ++ * deferred perimeter ++ CIRCLE SQUARE + effective perimeter ++ ++ redefine d 26 26

  27. Polymorphic data structures ( ELLIPSE ) ( CIRCLE ) ( POLYGON ) ( POLYGON ) ( CIRCLE ) figs : LIST [ FIGURE ] from from figs � start until figs � after loop loop figs � item � display figs � forth end 27 27

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