2015-02-26 35 views
1

我正在製作一個繪製程序,我可以製作矩形/橢圓。在該程序中,我可以移動/調整它們的大小,但也可以保存它們。實現調整大小/移動/保存訪問者模式

我的問題是現在我需要製作訪問者模式(調整大小/移動並保存)但我不知道我應該從哪裏開始。

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

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

抱歉不能給你,因爲我的名氣圖片...

+0

所以你所有的元素都有一個共同的超類型? ... –

+0

他們都繼承形狀 – Pklaas

回答

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

一個很好的參考是http://www.dofactory.com/net/visitor-design-pattern –

+0

@Pklaas但是我沒有得到使用這種模式的感覺像這樣...感覺將會使命令(移動,重製)的實施移動到訪問者。對於界面IVisitor中的各個形狀,您有更多的方法,並且形狀的具體實現會調用其適當的方法。 –

+0

@Pklaas像一個矩形有x1,y1,x2,y2,移動實現可以用dx和dy修正所有這些值,圓或橢圓可能有centerX,centerY和移動的實現只會修正2個值。 –