Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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);
Engine.AddBranch(ID1,ID2);

Finally the computation is performed like this:

Engine.DoSomeComputation();

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.

share|improve this question
    
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. –  csetzkorn 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
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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