我的要求是使用形状的名称并使用方法中的尺寸绘制该形状Draw('rectangle', 'l:10,w:20');
。
- 应该根据形状的类型验证尺寸。
- 可以重构这些类以添加更多类或更改层次结构。
- 不应使用像反射这样的运行时检查。这个问题只需要通过类设计来解决。
- 不要在客户端方法中使用
if-else
orswitch
语句Draw
。
要求:
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; }
}