C# 面向对象设计中类间的循环依赖
我一直坚持实现一个设计,因为它具有循环依赖性。我需要根据下面的代码片段生成一个shapes报告,我考虑将其实现为实现commonC# 面向对象设计中类间的循环依赖,c#,oop,design-patterns,C#,Oop,Design Patterns,我一直坚持实现一个设计,因为它具有循环依赖性。我需要根据下面的代码片段生成一个shapes报告,我考虑将其实现为实现commonIShape接口的Shape类和实现commonILanguage接口的Language类 这样一来,任何形状都会告诉自己名称,但它不知道语言,如果语言告诉任何形状的名称,那么它就不知道形状类型,也不知道它是否是单个形状或形状集合 但该报告是基于形状总数生成的,所以两个组之间似乎存在循环依赖关系 public string PrintShape(List<Shap
IShape
接口的Shape类和实现commonILanguage
接口的Language类
这样一来,任何形状都会告诉自己名称,但它不知道语言,如果语言告诉任何形状的名称,那么它就不知道形状类型,也不知道它是否是单个形状或形状集合
但该报告是基于形状总数生成的,所以两个组之间似乎存在循环依赖关系
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;
}
公共字符串PrintShape(列表形状,ILanguage语言)
{
字符串returnString=“”;
如果(shapes.Count>0)
{
returnString+=language.ReportText;
int numberSquares=shapes.OfType().Count();
int numberCircles=shapes.OfType().Count();
/*…这部分是独立的*/
如果(语言==“EN”)
{
returnString+=numberSquares+((numberSquares==1)?“Squares”:“Squares”);
returnString+=numberCircles+((numberCircles==1)?“圆圈”:“圆圈”);
}
其他的
{
returnString+=numberSquares+((numberSquares==1)?“Vierkant”:“Vierkanten”);
returnString+=numberCircles+((numberCircles==1)?“Cirkel”:“Cirkes”);
}
/*…这部分是独立的*/
returnString+=language.TotalText;
returnString+=shapes.Count+“”+language.shapeText;
}
返回字符串;
}
但我仍然无法决定如何获得文本圆形
,圆形
,方形
或维耶坎特
等,因为语言类不知道文本需要哪种形状。另外,如果语言类必须告诉shape/shapes文本,则添加新的形状需要修改每个语言类 现在若换一种方式,形状应该告诉它的名字,那个么它将不知道它应该用哪种语言
因此,这两个类之间存在循环类型依赖关系,我无法决定如何实现它。欢迎使用StackOverflow。你的问题是如何写的,很难准确地说出你有什么问题。标题暗示您有一个“循环依赖”,但您的方法不会递归地调用自身,因此不清楚为什么会出现这种情况。请编辑您的问题,以澄清您遇到的问题,即您得到的结果和预期结果。看看你是否需要这方面的建议。你问的问题很不清楚。由于您提到了循环依赖关系,如果您可以共享这两个类的代码或SolutionExplorer选项卡的屏幕截图,以便我们知道所提到的类在项目中的位置,这将非常有用。现在,如果我假设两个类都在不同的项目中(因此循环依赖),那么需要将这些项目的引用添加到第三个项目中,并从那里调用这两个类。也许可以添加一个输入列表和预期的字符串输出来帮助我们理解。也许我们可以为您提供一个更好的解决方案。我们仍在等待有人对此提供帮助。