Visitor 表示一个作用于某个对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public interface Element8

{9
void Accept(Visitor visitor);10
}11
}12

1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public class ConcereteElementA : Element8

{9
public ConcereteElementA()10

{11
}12

13

Element 成员#region Element 成员14

15
public void Accept(Visitor visitor)16

{17
visitor.VisitConcreteElementA(this);18
}19

20
#endregion21

22
public string OperationA()23

{24
return "This is in ConcereteElementA.OperationA()";25
}26
}27
}28

1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public class ConcereteElementB : Element8

{9

Element 成员#region Element 成员10

11
public void Accept(Visitor visitor)12

{13
visitor.VisitConcreteElementB(this);14
}15

16
#endregion17

18
public string OperationB()19

{20
return "This is in ConcereteElementB.OperationB()";21
}22
}23
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public interface Visitor8

{9
void VisitConcreteElementA(ConcereteElementA concereteElementA);10
void VisitConcreteElementB(ConcereteElementB concereteElementB);11
}12
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public class ConcereteVisitor1 : Visitor8

{9

Visitor 成员#region Visitor 成员10

11
public void VisitConcreteElementA(ConcereteElementA concereteElementA)12

{13
Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14
Console.WriteLine("we can visite ConcereteElementA's OperationA method"+ concereteElementA.OperationA());15
}16

17
public void VisitConcreteElementB(ConcereteElementB concereteElementB)18

{19
Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20
Console.WriteLine("we can visite ConcereteElementB's OperationB method");21
Console.WriteLine( concereteElementB.OperationB());22
}23

24
#endregion25
}26
}27

1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public class ConcereteVisitor2 : Visitor8

{9

Visitor 成员#region Visitor 成员10

11
public void VisitConcreteElementA(ConcereteElementA concereteElementA)12

{13
Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);14
Console.WriteLine("we can visite ConcereteElementA's OperationA method:"+ concereteElementA.OperationA());15
}16

17
public void VisitConcreteElementB(ConcereteElementB concereteElementB)18

{19
Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);20
Console.WriteLine("we can visite ConcereteElementB's OperationB method");21
Console.WriteLine( concereteElementB.OperationB());22
}23

24
#endregion25
}26
}27

1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Visitor6


{7
public class ObjectStructure8

{9
private System.Collections.ArrayList items = new System.Collections.ArrayList();10

11
public void Attach(Element element)12

{13
items.Add(element);14
}15

16
public void Detach(Element element)17

{18
items.Remove(element);19
}20

21
public void Accept(Visitor visitor)22

{23
foreach (Element e in items)24

{25
e.Accept(visitor);26
}27
}28
}29
}30

1
ObjectStructure os = new ObjectStructure();2
os.Attach(new ConcereteElementA());3
os.Attach(new ConcereteElementB());4

5
//create visitor objects6
ConcereteVisitor1 v1 = new ConcereteVisitor1();7
ConcereteVisitor2 v2 = new ConcereteVisitor2();8

9
//accepting visitors10
os.Accept(v1);11
os.Accept(v2);12

13
Console.ReadKey();

浙公网安备 33010602011771号