1
\$\begingroup\$

I have a class with three similar methods. I can't consider which a pattern to use better for refactoring: template method, strategy or something else? I don't know. Can you help me?
Update: This code is a little exercise to be a better programmer. I'm going to add some algorithms, but it's not a production code. I would write good not smelled code for myself. I would have a beautiful code from this, if it's possible.
The main goal of the code : is creating a graph, breadth-first search and breadth-first traversing in graph. I've used a Cormen &al. algorithms from "Introduction to Algorithms" book. The method named BreadthFirstSearch is search. The method named CleanVertex changes color of vertex after search from black to white again. The last method named Path is for traversing the hole graph. All vertexes are traversed and their order are saved in a variable and return.

public class Graph
{

    public bool BreadthFirstSearch(int value)
    {
        if (StartingPoint == null)
        {
            return false;
        }

        var queue = new Queue<Vertex>();
        queue.Enqueue(StartingPoint);
        while (queue.Count != 0)
        {
            var vertex = queue.Dequeue();
            if (vertex.Index == value)
            {
                CleanVertex();
                return true;
            }
            vertex.Color = Color.Black;
            for (int i = 0; i < vertex.Neighbours.Count; i++)
            {
                var vertexNeighbour = vertex.Neighbours[i];
                if (vertexNeighbour.Color == Color.White)
                {
                    vertexNeighbour.Color = Color.Grey;
                    queue.Enqueue(vertexNeighbour);
                }
            }
        }
        CleanVertex();
        return false;
    }

    private void CleanVertex()
    {
        if (StartingPoint == null)
        {
            return;
        }

        StartingPoint.Color = Color.White;
        var queue = new Queue<Vertex>();
        queue.Enqueue(StartingPoint);
        while (queue.Count != 0)
        {
            var vertex = queue.Dequeue();
            for (int i = 0; i < vertex.Neighbours.Count; i++)
            {
                var vertexNeighbour = vertex.Neighbours[i];
                if (vertexNeighbour.Color != Color.White)
                {
                    vertexNeighbour.Color = Color.White;
                    queue.Enqueue(vertexNeighbour);
                }
            }
        }
    }


    public string Path()
    {
        if (StartingPoint == null)
        {
            return string.Empty;
        }

        var queue = new Queue<Vertex>();
        queue.Enqueue(StartingPoint);
        var path = new StringBuilder();
        while (queue.Count != 0)
        {
            var vertex = queue.Dequeue();
            path.Append($"{vertex.Index} ");
            vertex.Color = Color.Black;
            for (int i = 0; i < vertex.Neighbours.Count; i++)
            {
                var vertexNeighbour = vertex.Neighbours[i];
                if (vertexNeighbour.Color == Color.White)
                {
                    vertexNeighbour.Color = Color.Grey;
                    queue.Enqueue(vertexNeighbour);
                }
            }
        }

        return path.ToString().Trim();
    }
}
\$\endgroup\$
  • \$\begingroup\$ Welcome to CodeReview@SE. In the first revision of this question, code indentation is unusual & inconsistent. I found creating code blocks bracketing them with "~~~-lines" less accident-prone than prefixing additional blanks. \$\endgroup\$ – greybeard Feb 13 at 8:45
  • \$\begingroup\$ @Galina. Please try to explain what the code is used for? Are you planning to add more algorithms? Is it used often? Where it is used? All of that can influence the advice you will get \$\endgroup\$ – Gilad Feb 13 at 15:26
  • \$\begingroup\$ Well, they are similar, but they are very different as well. They each return a different type and while the methods all sort of smell the same they also each have subtle differences. I guess I'd want to know what the goal is. Why are you worried about these routines? Is this a class exercise or do you have a real-world specific goal here? \$\endgroup\$ – Frank Merrow Feb 14 at 5:09
  • \$\begingroup\$ @greybeard I've tryed to correct. \$\endgroup\$ – Galina Feb 14 at 8:27
  • 1
    \$\begingroup\$ Others may disagree, but repeats like this appear in code from time to time. To be sure, if the "constant code" is complex enough that you find yourself fixing bugs in it and having to patch those bugs in multiple places, then definitely refactoring of some kind is in order. However, in this case the constant code is fairly simple and trying to factor it out will likely require some work that isn't worth in this case. \$\endgroup\$ – Frank Merrow Feb 15 at 0:11

Your Answer

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

Browse other questions tagged or ask your own question.