【发布时间】:2017-08-14 13:19:48
【问题描述】:
我的要求是使用 shape 的名称并使用 dimensions 绘制该形状,就像在方法 Draw('rectangle', 'l:10,w:20'); 中一样。
- 应根据形状类型验证尺寸。
- 可以重构这些类以添加更多类或更改层次结构。
- 不应使用像 reflection 这样的运行时检查。这个问题只需要通过类设计来解决。
- 不要在客户端方法
Draw中使用if-else或switch语句。
要求:
public static void main()
{
// Provide the shape and it's dimensions
Draw('rectangle', 'l:10,w:20');
Draw('circle', 'r:15');
}
我创建了以下类。我通过创建两个类层次结构来考虑低(松散)耦合和高内聚,这样它们就可以自行增长。我负责绘制一个类并生成另一个类的维度。
我的问题是关于创建这些对象并相互交互以实现我的要求。
public abstract class Shape()
{
Dimension dimension;
public void abstract SetDimentions(Dimension dimension);
public void abstract Draw()
}
public void Rectangle()
{
void override SetDimensions(RectangleDimension dimension)
{
}
void override Draw()
{
// Use the 'dimention' to draw
}
}
public void Circle()
{
void override SetDimensions(CircleDimension dimension)
{
}
void override Draw()
{
// Use the 'dimention' to draw
}
}
public class RectangleDimension
{
public int length {get; set; }
public int width { get; set; }
}
public class CircleDimension
{
public int circle { get; set; }
}
【问题讨论】:
标签: oop loose-coupling cohesion