I have a domain model, persisted in a database, which represents a graph. A graph consists of nodes (e.g. NodeTypeA, NodeTypeB) which are connected via branches. The two generic elements (nodes and branches will have properties). A graph will be sent to a computation engine. To perform computations the engine has to be initialised like so (simplified pseudo code):

Engine Engine = new Engine() ;
Object ID1 = Engine.AddNodeTypeA(TypeA.Property1, TypeA.Property2, …, TypeA.Propertyn);
Object ID2 = Engine.AddNodeTypeB(TypeB.Property1, TypeB.Property2, …, TypeB.Propertyn);

Finally the computation is performed like this:


I am just wondering, if there are any relevant design patterns out there, which help to achieve the above using good design principles. I hope this makes sense. Any feedback would be very much appreciated.

  • Closest thing to a 'pattern' I can suggest is MapReduce for things like this, not just as a framework of any sort, but rather the pattern of executing computation on partitionable parts like this. Even if it's not parallelized, having these 2 step segregation of actions is likely to make sense for your scenario, unless you're not looking for aggregates. – Jimmy Hoffa Sep 18 '12 at 13:48
  • What is your actual issue here? You have presented some sample code that isn't obviously wrong, and a very vague and abstract explanation of what it is trying to achieve. So, what is missing? What would a good answer to the question look like? – psr Sep 18 '12 at 19:15
  • @psr if I would know this, I would not ask. If my explanation is too vague please let me know what you do not understand. Basically, I have a graph which has to be expressed/translated into code like the one shown above. I imagine to have some kind of service which does this, by either traversing the graph and generating the code or extracting 'global' graph information from the graph (e.g. get all nodes, get neighbour nodes of node x). However, I am not sure about that so I ask the community before I re-implement the wheel. – cs0815 Sep 19 '12 at 7:49
  • @csetzkorn Your question is split into two parts - the first part talking about how you want to persist the graph, and the second part which talks about doing computations on the graph. It's not clear which part you're asking about; perhaps both? Also, what types of computations are you expecting? There's no easy "do it this way" answer here, the graph traversal / computation strategy will depend greatly on the problem being solved, so some extra information would help. – Daniel B Sep 26 '12 at 9:09

I think the Visitor Pattern is appropriate to your problem, as it allows you to separate the tasks:

  • apply a computation to every node on the graph; and
  • the computation that is applied.

Your DoSomeComputation() method would take a parameter that is a (closure/lambda/implementation of some Visitor interface[*]) and apply that to the nodes on the graph.

[*] delete as appropriate for the programming language you're using.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.