实现调整大小/移动/保存访客模式

时间:2015-02-26 10:31:42

标签: c# winforms visitor-pattern

我正在制作一个绘画程序,我可以制作矩形/椭圆。在该程序中,我可以移动/调整它们,但也可以保存它们。

我现在的问题是我需要制作访客模式(调整大小/移动并保存)但我不知道应该从哪里开始。

这些是我目前使用的方法:

public abstract void ResizeShape(PosSizableRect posSizableRect, float lastX, float lastY, float newX, float newY);
public abstract void MoveShape(int x, int y);
private void Write(List<Shape> shapes, StreamWriter streamWriter, string tabs = "")

抱歉因为我的声誉而无法给你拍照......

1 个答案:

答案 0 :(得分:1)

public interface IShape
{
    void Resize(PosSizableRect posSizableRect, float lastX, float lastY, float newX, float newY);
    void Move(int dx, int dy);
    void Write (StreamWriter writer, string tabs ="");
    void AcceptVisitor(IVisitor visitor);
}

public interface IVisitor
{
    void Visit(IShape shape);
}

这就是接口,现在是实现(一个例子)

public class MoveVisitor : IVisitor
{
    private int dx;
    private int dy;

    public MoveVisitor(int dx, int dy)
    {
         this.dx = dx;
         this.dy = dy;
    }
    public void Visit(IShape shape)
    {
         shape.Move(dx,dy);
    }
}