代理模式——代码版“吊丝的故事” Kevin

namespace ProxyDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //there is a shyboy's dream girl named xuxiao
            DreamGirl girl = new DreamGirl();
            girl.name = "xuxiao";

            //there is a shyboy name kevin kong,he love the girl,but he never said to her that he love her.
            ShyBoy shyBoy = new ShyBoy();
            shyBoy.name = "Kevin Kong";

            //once ,he want through he and the dream gril's commen friend,guimeng,to say he like her.
            GoodFriend goodFriend = new GoodFriend(shyBoy, girl);
            goodFriend.SendFlower();
            goodFriend.SendLoveMail();
            goodFriend.SendShortMessage();

            //finnally the shyboy never said his love to the girl and he lost her forever.
            
        }
    }
}

namespace ProxyDemo
{
    /// <summary>
    /// this is a pretty girl.
    /// </summary>
    class DreamGirl
    {
        public string name { get; set; }
    }
}

namespace ProxyDemo
{
    /// <summary>
    /// this boy want to pursuit the pretty girl but he doesn't dare.
    /// </summary>
    class ShyBoy:IPursuitMethod
    {
        public string name;
        public void SendLoveMail()
        {
            MessageBox.Show("Send Love Mail");
        }

        public void SendFlower()
        {
            MessageBox.Show("Send Love Flower");
        }

        public void SendShortMessage()
        {
            MessageBox.Show("Send Love ShortMessage");
        }
    }
}

namespace ProxyDemo
{
    //GM is they commen friend. 
    
    class GoodFriend:IPursuitMethod
    {
        private ShyBoy shyBoy;
        private DreamGirl girl;

        public GoodFriend(ShyBoy shyBoy,DreamGirl girl)
        {
            this.shyBoy = shyBoy;
            this.girl = girl;
        }

        public void SendLoveMail()
        {
            MessageBox.Show("Send Love Mail to "+girl.name+" for "+shyBoy.name);
        }

        public void SendFlower()
        {
            MessageBox.Show("Send Love Flower to " + girl.name + " for " + shyBoy.name);
        }

        public void SendShortMessage()
        {
            MessageBox.Show("Send Love Message to " + girl.name + " for " + shyBoy.name);
        }
    }
}

namespace ProxyDemo
{
    /// <summary>
    /// guys using those mothed to pursuit the pretty girl.
    /// </summary>
    interface IPursuitMethod
    {
         void SendLoveMail();

         void SendFlower();

         void SendShortMessage();

    }
}
原文地址:https://www.cnblogs.com/kfx2007/p/2793391.html