Tooled Composite Design Pattern Andy


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Tooled Composite Design Pattern Andy

  1. 1. Tooled Composite Design Pattern Andy Bulka Austhink Software [email_address]
  2. 2. By GOF author John Vlissides
  3. 3. The Problem
  4. 4. “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>
  5. 5. Representing shapes COMPOSITE – design pattern
  6. 6. 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>
  7. 7. Representing “Tools” STATE – design pattern
  8. 8. 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><ul><li>Answer -> with the visitor pattern </li></ul>
  9. 9. 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>
  10. 10. 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
  11. 11. 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>
  12. 12. 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() ...
  13. 13. 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><ul><li>Also - instead of RTTI just have each tool return an enum or string from its GetMyType() method or something similar – avoids overhead (if any) of RTTI and is under programmers complete control. </li></ul>
  14. 14. 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
  15. 15. 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>
  16. 16. State Pattern – switching state Notice calls to “SetTool”
  17. 17. 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>
  18. 18. 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>
  19. 19. Final Pattern
  20. 20. 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>
  21. 21. 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>