类图
待访问对象的抽象
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?