mercredi 12 février 2020

Refactoring 1 class with 3 similar methods with different signature and return value

I have a class with 3 similar methods. I can't consider which pattern to use better for refactoring: template method, strategy or something else? Or maybe to create a lof o I don't know. Can you help me? I don't insert the code of third method because it's like a second (has a little difference and return type string) and because of restriction of service error (a lot of code, please add more details)

    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;
        }

        var queue = new Queue<Vertex>();
        queue.Enqueue(StartingPoint);
        StartingPoint.Color = Color.White;

        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);
                }
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire