Visitor 表示一个作用于某个对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作...

ContractedBlock.gif ExpandedBlockStart.gif Element
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface Element
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        void Accept(Visitor visitor);
10ExpandedSubBlockEnd.gif    }

11ExpandedBlockEnd.gif}

12None.gif
ContractedBlock.gif ExpandedBlockStart.gif ConcereteElementA
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ConcereteElementA : Element
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public ConcereteElementA()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11ExpandedSubBlockEnd.gif        }

12InBlock.gif
13ContractedSubBlock.gifExpandedSubBlockStart.gif        Element 成员#region Element 成员
14InBlock.gif
15InBlock.gif        public void Accept(Visitor visitor)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            visitor.VisitConcreteElementA(this);
18ExpandedSubBlockEnd.gif        }

19InBlock.gif
20ExpandedSubBlockEnd.gif        #endregion

21InBlock.gif
22InBlock.gif        public string OperationA()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            return "This is in ConcereteElementA.OperationA()";
25ExpandedSubBlockEnd.gif        }

26ExpandedSubBlockEnd.gif    }

27ExpandedBlockEnd.gif}

28None.gif
ContractedBlock.gif ExpandedBlockStart.gif ConcereteElementB
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ConcereteElementB : Element
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ContractedSubBlock.gifExpandedSubBlockStart.gif        Element 成员#region Element 成员
10InBlock.gif
11InBlock.gif        public void Accept(Visitor visitor)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            visitor.VisitConcreteElementB(this);
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16ExpandedSubBlockEnd.gif        #endregion

17InBlock.gif
18InBlock.gif        public string OperationB()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            return "This is in ConcereteElementB.OperationB()";
21ExpandedSubBlockEnd.gif        }

22ExpandedSubBlockEnd.gif    }

23ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Visitor
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public interface Visitor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        void VisitConcreteElementA(ConcereteElementA concereteElementA);
10InBlock.gif        void VisitConcreteElementB(ConcereteElementB concereteElementB);
11ExpandedSubBlockEnd.gif    }

12ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif ConcereteVisitor1
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ConcereteVisitor1 : Visitor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ContractedSubBlock.gifExpandedSubBlockStart.gif        Visitor 成员#region Visitor 成员
10InBlock.gif
11InBlock.gif        public void VisitConcreteElementA(ConcereteElementA concereteElementA)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);
14InBlock.gif            Console.WriteLine("we can visite ConcereteElementA's OperationA method"+ concereteElementA.OperationA());
15ExpandedSubBlockEnd.gif        }

16InBlock.gif
17InBlock.gif        public void VisitConcreteElementB(ConcereteElementB concereteElementB)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);
20InBlock.gif            Console.WriteLine("we can visite ConcereteElementB's OperationB method");
21InBlock.gif            Console.WriteLine( concereteElementB.OperationB());
22ExpandedSubBlockEnd.gif        }

23InBlock.gif
24ExpandedSubBlockEnd.gif        #endregion

25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}

27None.gif
ContractedBlock.gif ExpandedBlockStart.gif ConcereteVisitor2
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif   public  class ConcereteVisitor2 : Visitor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9ContractedSubBlock.gifExpandedSubBlockStart.gif        Visitor 成员#region Visitor 成员
10InBlock.gif
11InBlock.gif        public void VisitConcreteElementA(ConcereteElementA concereteElementA)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            Console.WriteLine("{0} visited by {1}", concereteElementA.GetType().Name, this, GetType().Name);
14InBlock.gif            Console.WriteLine("we can visite ConcereteElementA's OperationA method:"+ concereteElementA.OperationA());
15ExpandedSubBlockEnd.gif        }

16InBlock.gif
17InBlock.gif        public void VisitConcreteElementB(ConcereteElementB concereteElementB)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            Console.WriteLine("{0} visited by {1}", concereteElementB.GetType().Name, this, GetType().Name);
20InBlock.gif            Console.WriteLine("we can visite ConcereteElementB's OperationB method");
21InBlock.gif            Console.WriteLine( concereteElementB.OperationB());
22ExpandedSubBlockEnd.gif        }

23InBlock.gif
24ExpandedSubBlockEnd.gif        #endregion

25ExpandedSubBlockEnd.gif    }

26ExpandedBlockEnd.gif}

27None.gif
ContractedBlock.gif ExpandedBlockStart.gif ObjectStructure
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace Gof.Test.Visitor
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class ObjectStructure
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        private System.Collections.ArrayList items = new System.Collections.ArrayList();
10InBlock.gif
11InBlock.gif        public void Attach(Element element)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            items.Add(element);
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16InBlock.gif        public void Detach(Element element)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            items.Remove(element);
19ExpandedSubBlockEnd.gif        }

20InBlock.gif
21InBlock.gif        public void Accept(Visitor visitor)
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            foreach (Element e in items)
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                e.Accept(visitor);
26ExpandedSubBlockEnd.gif            }

27ExpandedSubBlockEnd.gif        }

28ExpandedSubBlockEnd.gif    }

29ExpandedBlockEnd.gif}

30None.gif
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gif            ObjectStructure os = new ObjectStructure();
 2None.gif            os.Attach(new ConcereteElementA());
 3None.gif            os.Attach(new ConcereteElementB());
 4None.gif
 5None.gif            //create visitor objects
 6None.gif            ConcereteVisitor1 v1 = new ConcereteVisitor1();
 7None.gif            ConcereteVisitor2 v2 = new ConcereteVisitor2();
 8None.gif
 9None.gif            //accepting visitors
10None.gif            os.Accept(v1);
11None.gif            os.Accept(v2);
12None.gif
13None.gif            Console.ReadKey();

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/01/17/622625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值