vendredi 25 novembre 2016

Best Object Oriented approach to solve this simple scenario

I'm not so sure about what's the best approach to model this problem in the object oriented world.

Suppose I have to represent a graph and it's nodes in Java and suppose I to visit the graph using a depth first search (DFS).

What the best approach in Software engineering among these two:

  • Create a class Node, a class Graph and a class GraphsFunctions where the GraphsFunctions class contains the DFS method which takes the Graph and a Node as parameters. Something like this: public void DFS(Graph g, Node n) and the call new GraphsFunctions().DFS(new Graph(), new Node())

  • Create a class Node, a class Graph where the Graph class contains the DFS method which takes only the Node as parameter. Something like this: public void DFS(Node n) and the call new Graph().DFS(new Node())

I would rather use the first of the two options but I can't say why it's the best for me. Can you tell me what makes the best choice, actually the best?

Aucun commentaire:

Enregistrer un commentaire