Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tooled Composite Design Pattern

  • Be the first to comment

  • Be the first to like this

Tooled Composite Design Pattern

  1. 1. Tooled Composite Design Pattern Presentation by Andy Bulka CTO Austhink Software [email_address]
  2. 2. What is this about? <ul><li>Ever wanted to create a &quot;direct manipulation&quot; program where you select various tools and manipulate stuff on a workspace?  Like MS paint etc. These sorts of applications are quite difficult to build due to the many possible combinations of behavior that is needed.  Every tool must know what to do with each type of object.  How do we manage this sort of complexity? </li></ul>
  3. 3. By GOF author John Vlissides
  4. 4. The Problem
  5. 5. “ direct manipulation” metaphor. <ul><li>How do you represent shapes? </li></ul><ul><li>How do you represent tools? </li></ul><ul><li>How do tools and shapes interact? </li></ul><ul><li>How do you enhance the editor with new shapes and tools? </li></ul>
  6. 6. Representing shapes COMPOSITE – design pattern
  7. 7. Representing “Tools” <ul><li>usually a palette of tools </li></ul><ul><li>editor’s behavior changes with the current tool </li></ul><ul><li>E.g. when drawing tool is active we create shapes; </li></ul><ul><li>E.g. when the selection tool is active we select shapes </li></ul>
  8. 8. Representing “Tools” STATE – design pattern
  9. 9. How tools and shapes interact <ul><li>How a tool interacts with each shape is usually different </li></ul><ul><li>m x n possible interactions between m tools and n shapes. </li></ul><ul><li>How do we manage and organise this complexity? </li></ul>
  10. 10. Visitor
  11. 11. Visitor <ul><li>Shapes don’t have tool knowledge – tools do all the work. Shapes just implement AcceptVisitor(v) and then do a v.VisitShape() or v.VisitEdge() or whatever they themselves are. </li></ul><ul><li>Each tool has a VisitShape VisitEdge VisitShapeRhEdgeZone method that gets triggered in this way. </li></ul>
  12. 12. Example Sequence <ul><li>Start in Hover Tool </li></ul><ul><li>All mouseMove events go to Hover tool </li></ul><ul><li>As hover over shapes/edges you ask what is under me and change cursor. You “visit” the shape and change cursor accordingly </li></ul>ToolHover -------------- VisitShape() cursor = HAND VisitEdge() cursor = ARROW
  13. 13. Example Sequence continued <ul><li>User Left Clicks </li></ul><ul><li>HoverTool.OnLeftClick sees that you are over a resize zone shape, so switches to the Resize tool Zones (e.g. resize zone) within shapes are also ‘shapes’ </li></ul><ul><li>Resize tool.OnMouseMove resizes the shape you are on. Repeatedly (as MouseMove events arrive). </li></ul><ul><li>Resize tool.OnMouseUp switches back to the hover tool. </li></ul>
  14. 14. RTTI as alternative to Visitor <ul><li>Have each tool use RTTI (runtime type info) to see what the type of the shape is and do something. </li></ul><ul><li>Thus instead of each tool with numerous VisitSOMETHING() method, just have a single Visit() method with an if statement based on rtti inside. </li></ul>ToolHover -------------- Visit() if target == Shape // use of RTTI … else if target == Edge ….. ToolHover -------------- VisitShape() … VisitEdge() ...
  15. 15. RTTI as alternative to Visitor <ul><li>When visitor was invented double dispatch was the only way to get around the lack of RTTI in C++ </li></ul><ul><li>RTTI approach is simpler than visitor </li></ul><ul><li>Easier to reuse and specialise tools since don’t have to modify visitor class every time add new shape – just subclass a tool and use RTTI </li></ul>
  16. 16. Events <ul><li>Funnel all events through to the current tool . </li></ul><ul><li>Each tool has custom handling for all the gui events e.g. mouseDown, mouseClick, mouseMove etc. </li></ul>Classic STATE pattern, passing through method invocations to the current state object
  17. 17. Tooled Composite pattern Rewire events as you swap tools SwapTool(tool)
  18. 18. Event Handling <ul><li>MouseUp might trigger exiting a tool and reverting to another tool e.g. back to Hover. </li></ul><ul><li>State pattern – each state knows when to switch to another state – OR – outer class e.g. canvas knows </li></ul>
  19. 19. State Pattern – switching state Notice calls to “SetTool”
  20. 20. Prototype Pattern <ul><li>Use for creation tool </li></ul><ul><li>Create a copy of an instance of an object </li></ul><ul><li>Could create a new instance rather than prototype – depends on how complex the prototypical object is </li></ul>
  21. 21. Command Pattern <ul><li>Hook in command manager for undo/redo </li></ul><ul><li>We use tool to generate a command and then run the command, which redoes the gui action, except through “official channels”  </li></ul>
  22. 22. Final Pattern
  23. 23. Reflections <ul><li>Classic approach -> visitor. Practical approach -> use RTTI (or equivalent e.g. have each shape return a shapeType enum) for better comprehensibility. </li></ul><ul><li>Classic approach -> 3D table of possibilities, with events, shapes, tools on each axis. Practical approach -> table too sparse and complex, so just code for the cases you want. </li></ul>
  24. 24. Reflections <ul><li>Classic approach -> some blend of visitShape() / visitEdge() etc methods and mouse event methods, within each tool Practical approach -> Skip most of the visit methods and do the logic in the mouse handling methods. Generalise the mouse handling into one event (mouseAction) and use if statements to catch the situations of interest. You know what the current shape is by having a pointer to it (set up for you by the tool or something). </li></ul>