Your SlideShare is downloading. ×
Tooled Composite Design Pattern   Andy
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Tooled Composite Design Pattern Andy

617
views

Published on

Published in: Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
617
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Tooled Composite Design Pattern Andy Bulka Austhink Software [email_address]
  • 2. By GOF author John Vlissides http://www.research.ibm.com/designpatterns/pubs/ph-sep99.pdf
  • 3. The Problem
  • 4. “direct manipulation” metaphor.
    • How do you represent shapes?
    • How do you represent tools?
    • How do tools and shapes interact?
    • How do you enhance the editor with new shapes and tools?
  • 5. Representing shapes COMPOSITE – design pattern
  • 6. Representing “Tools”
    • usually a palette of tools
    • editor’s behavior changes with the current tool
    • E.g. when drawing tool is active we create shapes;
    • E.g. when the selection tool is active we select shapes
  • 7. Representing “Tools” STATE – design pattern
  • 8. How tools and shapes interact
    • How a tool interacts with each shape is usually different
    • m x n possible interactions between m tools and n shapes.
    • How do we manage and organise this complexity?
    • Answer -> with the visitor pattern
  • 9. Visitor
    • 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.
    • Each tool has a VisitShape VisitEdge VisitShapeRhEdgeZone method that gets triggered in this way.
  • 10. Example Sequence
    • Start in Hover Tool
    • All mouseMove events go to Hover tool
    • As hover over shapes/edges you ask what is under me and change cursor. You “visit” the shape and change cursor accordingly
    ToolHover -------------- VisitShape() cursor = HAND VisitEdge() cursor = ARROW
  • 11. Example Sequence continued
    • User Left Clicks
    • 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’
    • Resize tool.OnMouseMove resizes the shape you are on. Repeatedly (as MouseMove events arrive).
    • Resize tool.OnMouseUp switches back to the hover tool.
  • 12. RTTI as alternative to Visitor
    • Have each tool use RTTI (runtime type info) to see what the type of the shape is and do something.
    • Thus instead of each tool with numerous VisitSOMETHING() method, just have a single Visit() method with an if statement based on rtti inside.
    ToolHover -------------- Visit() if target == Shape // use of RTTI … else if target == Edge ….. ToolHover -------------- VisitShape() … VisitEdge() ...
  • 13. RTTI as alternative to Visitor
    • When visitor was invented double dispatch was the only way to get around the lack of RTTI in C++
    • RTTI approach is simpler than visitor
    • 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
    • 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.
  • 14. Events
    • Funnel all events through to the current tool .
    • Each tool has custom handling for all the gui events e.g. mouseDown, mouseClick, mouseMove etc.
    Classic STATE pattern, passing through method invocations to the current state object
  • 15. Event Handling
    • MouseUp might trigger exiting a tool and reverting to another tool e.g. back to Hover.
    • State pattern – each state knows when to switch to another state – OR – outer class e.g. canvas knows
  • 16. State Pattern – switching state Notice calls to “SetTool”
  • 17. Prototype Pattern
    • Use for creation tool
    • Create a copy of an instance of an object
    • Could create a new instance rather than prototype – depends on how complex the prototypical object is
  • 18. Command Pattern
    • Hook in command manager for undo/redo
    • We use tool to generate a command and then run the command, which redoes the gui action, except through “official channels” 
  • 19. Final Pattern
  • 20. Reflections
    • Classic approach -> visitor. Practical approach -> use RTTI (or equivalent e.g. have each shape return a shapeType enum) for better comprehensibility.
    • 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.
  • 21. Reflections
    • 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).