mardi 21 février 2017

Cyclic dependency in design pattern

I am stuck with implementing one of design since it has cyclic dependency. I need to generate a shapes report as per below code snippet and I thought of implementing this as separating as Shape classes which implements common IShape interface and Language classes which implements common ILanguage interface. This way any shape will tell its name itself but it doesn't know about language, and if language tells any shape's name then it doesn't know about shape type and also if it is a single shape or collection of shapes. But report is generated based on total number of Shapes so there seems to be circular dependency between both groups. Can someone help to solve this. Thanks in advance.

public string PrintShape(List<Shape> shapes, string language)
{
    string returnString = "";

    if (shapes.Count == 0)
    {
        returnString = (language == "EN") ? "Empty list of shapes" : "Lege lijst van vormen";
    }
    else
    {
        returnString += (language == "EN") ? "Shapes report: " : "Samenvatting vormen: ";

        int numberSquares = 0, numberCircles = 0;

        for (int i = 0; i < shapes.Count; i++)
        {
            if (shapes[i].type == "SQUARE")
            {
                numberSquares++;
            }
            if (shapes[i].type == "CIRCLE")
            {
                numberCircles++;
            }
        }

        if (language == "EN")
        {
            returnString += numberSquares + ((numberSquares == 1) ? "Square " : "Squares ");
            returnString += numberCircles + ((numberCircles == 1) ? "Circle " : "Circles ");
        }
        else
        {
            returnString += numberSquares + ((numberSquares == 1) ? "Vierkant " : "Vierkanten ");
            returnString += numberCircles + ((numberCircles == 1) ? "Cirkel " : "Cirkels ");
        }

        returnString += (language == "EN") ? "TOTAL: " : "TOTAAL: ";
        returnString += (numberCircles + numberSquares) + " " + (language == "EN" ? "shapes" : "vormen");
    }

    return returnString;
}

Aucun commentaire:

Enregistrer un commentaire