![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Element
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public interface Element
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
void Accept(Visitor visitor);
10
}
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ConcereteElementA
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class ConcereteElementA : Element
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
public ConcereteElementA()
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
}
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Element 成员#region Element 成员
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
public void Accept(Visitor visitor)
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
visitor.VisitConcreteElementA(this);
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
#endregion
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
public string OperationA()
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
return "This is in ConcereteElementA.OperationA()";
25
}
26
}
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ConcereteElementB
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class ConcereteElementB : Element
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Element 成员#region Element 成员
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void Accept(Visitor visitor)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
visitor.VisitConcreteElementB(this);
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
#endregion
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
public string OperationB()
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
return "This is in ConcereteElementB.OperationB()";
21
}
22
}
23
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Visitor
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public interface Visitor
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
void VisitConcreteElementA(ConcereteElementA concereteElementA);
10
void VisitConcreteElementB(ConcereteElementB concereteElementB);
11
}
12
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ConcereteVisitor1
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class ConcereteVisitor1 : Visitor
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Visitor 成员#region Visitor 成员
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void VisitConcreteElementA(ConcereteElementA concereteElementA)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public void VisitConcreteElementB(ConcereteElementB concereteElementB)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
#endregion
25
}
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ConcereteVisitor2
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class ConcereteVisitor2 : Visitor
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Visitor 成员#region Visitor 成员
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void VisitConcreteElementA(ConcereteElementA concereteElementA)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public void VisitConcreteElementB(ConcereteElementB concereteElementB)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
#endregion
25
}
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ObjectStructure
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace Gof.Test.Visitor
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public class ObjectStructure
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
private System.Collections.ArrayList items = new System.Collections.ArrayList();
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
public void Attach(Element element)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
items.Add(element);
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public void Detach(Element element)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
items.Remove(element);
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public void Accept(Visitor visitor)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
foreach (Element e in items)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
e.Accept(visitor);
26
}
27
}
28
}
29
}
30![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
客户代码
1
ObjectStructure os = new ObjectStructure();
2
os.Attach(new ConcereteElementA());
3
os.Attach(new ConcereteElementB());
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
//create visitor objects
6
ConcereteVisitor1 v1 = new ConcereteVisitor1();
7
ConcereteVisitor2 v2 = new ConcereteVisitor2();
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
//accepting visitors
10
os.Accept(v1);
11
os.Accept(v2);
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
Console.ReadKey();
posted @
2007-01-17 13:41
南守拥
阅读(
487)
评论()
编辑
收藏
举报