我有一個工廠返回類型IShape
。工廠建立像Circle
,Square
等具體類如下。使用工廠模式創建的類型
public class ShapeFactory
{
public IShape GetShape(Shape shape, string name)
{
IShape s = null;
switch (shape)
{
case Shape.Square: s = new Square(name);
break;
case Shape.Triangle: s = new Triagle(name);
break;
case Shape.Circle: s = new Circle(name);
break;
}
return s;
}
}
用戶可以編輯任何I型,但每個具體類都需要不同的對話框來編輯各種屬性。檢測Shape的最佳方式是什麼? 目前,我有一個void Edit(IShape shape)
方法,執行了一系列的檢查,如果
public static void Edit(IShape shape)
{
if (shape == Square)
new EditSquare(shape as Square).ShowDialog();
else if (shape == Triangle)
new EditTriangle(shape as Triangle).ShowDialog();
}
這是一個合理的方法呢?什麼是最好的方式去做這件事。
編輯: 每個具體類從抽象形狀類派生定義一些基本屬性
public abstract class Shape
{
public string Name { get; set; }
public Rect Boundary { get; }
public double Area { get; protected set; }
}
IShape的暴露了一些功能,用戶想要執行
public interface IShape
{
void Translate(Vector v);
void Calculate();
}
由於
你能告訴我們IShape接口嗎?而你的具體類(Square,Circle等)則從基類(Shape)派生。 – Steve 2012-03-04 10:14:53
你應該在代碼審查中發佈這個。 – Lloyd 2012-03-04 10:15:56