在我的winform项目中,我有一些图形模型类:Rectangle
,Assosiation line
和Text
。
示例:
public class Rectangle
{
public short Id { get; set; }
public short Zindex { get; set; }
public Color BackColor { get; set; }
public bool Selected { get; set; }
public Size Size{ get; set; }
}
常见的Inteface IPathBuilder
包含为每个模型生成图形路径的类:RectangleGraphicPathBuilder
等。示例:
public class RectangleGraphicPathBuilder : IPathBuilder
{
protected override GraphicsPath Build(IShape inShape )
{
var shape = inShape as Rectangle;
var newPath = new GraphicsPath();
newPath.AddRectangle(new Rectangle(shape.Location.X, shape.Location.Y, shape.Size.Width, shape.Size.Height));
return newPath;
}
}
我需要使用一些调用这些构建器的模式,而集合中的形状会迭代。像:
forech(IShape shape in shapeColection){
var path = IPathBuilder.Builder(shape);
}
我很高兴有任何提示。
答案 0 :(得分:0)
将方法AddShapeToPath
添加到IShape
界面,该界面将专门用于每个形状。在构建器中调用此方法,而不是尝试处理每个形状类型。
public interface IShape {
void AddShapeToPath(GraphicsPath path);
}
public class Rectangle: IShape
{
// properties removed for readability
public void AddShapeToPath(GraphicsPath path)
{
path.AddRectangle(new Rectangle(Location.X, Location.Y, Size.Width, Size.Height));
}
}
protected override GraphicsPath Build(IShape inShape )
{
var newPath = new GraphicsPath();
inShape.AddShapeToPath(newPath);
return newPath;
}
答案 1 :(得分:0)
我认为你需要策略设计模式。 Samy已经提供了战略设计模式实施的一个例子。