Question

My program is attempting to draw grammars in C# & WPF. I have:

1 DataStructure project which describes a tree structure of how it should be visualised. Each node in the tree relates to a symbol in the grammar. Nodes at the top define the rule for that nonterminal symbol.

1 Drawer project which describes the user controls in WPF.

I need to reference drawer in my datastructure as when i traverse the tree, I call DataStructure.draw(); on each node. I also need to reference the datastructure in my drawer project so I can respond to a user clicking on my GUI, it will update the data structure.

This creates a circular depedency, I have tried to use a controller class but I have no idea :/

Was it helpful?

Solution

You should look at the Visitor pattern, described here:

http://en.wikipedia.org/wiki/Visitor_pattern

This will allow your datastructure project to accept a visitor of any type, including a drawer, but the implementatin of the actual visitor (in your case the drawing logic) to live separately with no dependency in the way you don't want.

OTHER TIPS

I need to reference drawer in my datastructure as when i traverse the tree, I call DataStructure.draw();

Quick fix: traverse and call HelperObject.Draw(DataStructure);

Your DataStructure shouldn't know how to Draw itself, pretend it has to be re-used in a WinForms or other platform. HelperObject could be part of the GUI.

Extract common functionality out of your objects until a third layer of abstraction. You may also want to look at MVC where your grammer would be the Model, your drawer would be the View, and you still need the controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top