面向对象设计中类之间的循环依赖

时间:2017-02-21 16:55:45

标签: c# oop design-patterns

由于它具有循环依赖性,我很难实现其中一个设计。我需要根据下面的代码片段生成一个形状报告,我想把它实现为分离为实现公共IShape接口的Shape类和实现公共ILanguage接口的语言类。

这样任何形状都会告诉它的名字本身,但它不知道语言,如果语言告诉任何形状的名称,那么它不知道形状类型,也不知道它是单个形状还是形状集合。

但报告是根据形状总数生成的,因此两组之间似乎存在循环依赖关系。

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

这里我用面向对象设计简化了这个方法。

以下是语言实施的代码段:

public interface ILanguage { string TotalText { get; } /*..& few others..*/ }

public class English : ILanguage
{
    public string TotalText { get { return "TOTAL:"; } /*..& few others..*/ }
}

public class Dutch : ILanguage
{
    public string TotalText { get { return "TOTAAL:"; } /*..& few others..*/ }
}

这里是形状实现:

public interface IShape { double Area();    }

public class Square : IShape
{
    public double Area() { /*..area calculation..*/; }
}

public class Circle : IShape
{
    public double Area() { /*..area calculation..*/; }
}

现在报告方法PrintShape将简化如下:

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

    if (shapes.Count > 0)
    {
        returnString += language.ReportText;

        int numberSquares = shapes.OfType<Square>().Count();
        int numberCircles = shapes.OfType<Circle>().Count();

        /*..this part is dependent..*/
        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 ");
        }
        /*..this part is dependent..*/

        returnString += language.TotalText;
        returnString += shapes.Count + " " + language.ShapesText;
    }

    return returnString;
}

但我仍然无法决定如何获取文字CircleCirclesSquareVierkanten等,因为语言课程不知道哪种类型形状的文本是必要的。
此外,如果语言类必须告诉形状/形状文本,则添加新形状将需要修改每个语言类。

现在如果采取其他方式,形状应该告诉它的名字然后它将不知道它应该是哪种语言。

因此两个类之间存在循环类型依赖关系,我无法决定如何实现它。

0 个答案:

没有答案