Search code examples
c#winformsvisitor-pattern

Implement a Resize/Move/Save Visitor Pattern


I am making a paint program where i can make Rectangle/Ellipse. In that program i can move/resize them but also save them.

My problem is now that i need to make a visitor pattern(Resize/Move and save) But I don't know where i should start.

These are the method i use at the moment:

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 = "")

sorry can't give you pictures because of my reputation...


Solution

  • 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);
    }
    

    Thats the interfaces, now the implementation (one example)

    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);
        }
    }