访问者模式 VisitorPattern

本文通过一个具体的访问者模式实现案例,展示了如何定义访问对象、访问状态及调用者的具体实现方式。通过不同类型的访问对象与多种访问状态的组合使用,实现了多样化的交互逻辑。

待访问对象的抽象

using System; public abstract class NiceGirl { private string _Name, _Age, _HairColor, _SkinColor, _EyesColor, _Clothes, _Shoes, _Bag; public NiceGirl(string name, string age, string haircolor, string skincolor, string eyescolor, string clothes, string shoes, string bag) { _Name = name; _Age = age; _HairColor = haircolor; _SkinColor = skincolor; _EyesColor = eyescolor; _Clothes = clothes; _Shoes = shoes; _Bag = bag; } public string Name { get { return _Name; } } public string Age { get { return _Age; } } public string HairColor { get { return _HairColor; } } public string SkinColor { get { return _SkinColor; } } public string EyesColor { get { return _EyesColor; } } public string Clothes { get { return _Clothes; } } public string Shoes { get { return _Shoes; } } public string Bag { get { return _Bag; } } public abstract void ReceiveLetter(SweatTalk sweattalk); }

待访问对象A

using System; public class EastNiceGirl : NiceGirl { public EastNiceGirl(string name, string age, string haircolor, string skincolor, string eyescolor, string clothes, string shoes, string bag) : base(name, age, haircolor, skincolor, eyescolor, clothes, shoes, bag) { } public override void ReceiveLetter(SweatTalk sweattalk) { Console.WriteLine(sweattalk.TalkToEastNiceGirl(this)); } }

待访问对象B

using System; public class WestNiceGirl : NiceGirl { public WestNiceGirl(string name, string age, string haircolor, string skincolor, string eyescolor, string clothes, string shoes, string bag) : base(name, age, haircolor, skincolor, eyescolor, clothes, shoes, bag) { } public override void ReceiveLetter(SweatTalk sweattalk) { Console.WriteLine(sweattalk.TalkToWestNiceGirl(this)); } }

访问状态抽象

public interface SweatTalk { string TalkToWestNiceGirl(NiceGirl nicegirl); string TalkToEastNiceGirl(NiceGirl nicegirl); }

访问状态A

using System; using System.Text; public class SweatTalkA : SweatTalk { #region SweatTalk 成员 public string TalkToWestNiceGirl(NiceGirl nicegirl) { string tmp; tmp = string.Format("{0} i love you!", nicegirl.Name); return tmp; } public string TalkToEastNiceGirl(NiceGirl nicegirl) { string tmp; tmp = string.Format("{0} i am so happy i can make friend with you,if you don't mind,i want keep you forever!", nicegirl.Name); return tmp; } #endregion }

访问状态B

using System; public class SweatTalkB : SweatTalk { #region SweatTalk 成员 public string TalkToWestNiceGirl(NiceGirl nicegirl) { string tmp; tmp = string.Format("{0}!Can you marry me?",nicegirl.Name); return tmp; } public string TalkToEastNiceGirl(NiceGirl nicegirl) { string tmp; tmp = string.Format("{0}!i want to sacrifice myself to look after you!Please marry me!",nicegirl.Name); return tmp; } #endregion }

调用者

using System; using System.Collections.Generic; using System.Text; namespace VisitorPattern { class Program { static void Main(string[] args) { NiceGirl eastGirl = new EastNiceGirl("Xiao Xue", "20", "black", "yellow", "black", "red t-shirt and jeans trouses", "white shoes", "red bag"); NiceGirl westGirl = new WestNiceGirl("Lucy", "21", "gold-yellow", "white", "blue", "red t-shirt and jeans trouses", "white shoes", "red bag"); SweatTalk sweattalkA = new SweatTalkA(); eastGirl.ReceiveLetter(sweattalkA); westGirl.ReceiveLetter(sweattalkA); SweatTalk sweattalkB = new SweatTalkB(); eastGirl.ReceiveLetter(sweattalkB); westGirl.ReceiveLetter(sweattalkB); Console.ReadKey(); } } }

执行结果:

Xiao Xue i am so happy i can make friend with you,if you don't mind,i want keep
you forever!
Lucy i love you!
Xiao Xue!i want to sacrifice myself to look after you!Please marry me!
Lucy!Can you marry me?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值