views:

77

answers:

1

Hi,

I am writing the following program and I am thinking of a good design for it in C++

Problem:
I am writing a graph library. There is Graph<E> class.

Different graph algorithms have different types of information stored on the edges (Potentials, Cost, color, flow values, capacities etc.) For each algorithm, there is a different EdgeClass that is passed as a template argument to the Graph class.

Now what I want to do is that after each step of the algorithm, I want to write out the DOT file for the graph to be used by Graphviz. (This way I can generate graphs, for teaching purposes). However, the graphs should have the edges colored(and other possible attributes set) in different ways at each step. When and how the edge should be colored (or other attributes set), depends on the algorithm.

The EdgeClass cannot store any Graph drawing related information as the DOT drawing facility is more like a debugging facility. However, once the attributes of the edge are known, the algorithm for writing the DOT file for a graph object is very generic and can be put in the Graph class itself. I can write a function called dot() in the Graph<E> class that writes out the DOT for the graph.

What design pattern should I use that allows the graph algorithm (like shortest path) to set the attributes of the edges so that when the dot() function for the graph class is called it displays the information inside the EdgeClass as well as the algorithm specific edge attributes also in the DOT file without forcing me to store these additional graph related attributes in the EdgeClass itself? How can I achieve this decoupling?

A: 

I recommend the Strategy pattern
It looks like what you need.

Also, as far as I understood, you can separate the graphical setup data into independent structure.
I think, this makes sense as well: you can use these presets later for different graphs.

Good luck!

avp