设计模式

设计模式 - 代理模式

2016-01-25  本文已影响65人  Mitchell

代理模式

using System;
namespace Factory1
{
    //代理类提供的接口
    interface IGiveGift
    {
        void GiveDolls();
        void GiveFlowers();
        void GiveChocolate();
    }

    //接受者
    class SchoolGirl{

        private string name;
        public string Name
        {
            get{ return name;}
            set{ name = Name;}
        }
    }

    //追求者
    class Pursuit:IGiveGift
    {
        SchoolGirl mm;
        public Pursuit(SchoolGirl mm)
        {
            this.mm = mm;
        }
        public void GiveDolls()
        {
            Console.WriteLine (mm.Name + "give dolls");
        }
        public void GiveFlowers()
        {
            Console.WriteLine (mm.Name + "give your fllowers");
        }
        public void GiveChocolate()
        {
            Console.WriteLine (mm.Name + "give your chocolate");
        }
    }

    //代理类
    class Proxy:IGiveGift
    {
        Pursuit gg;
        //创建追求者
        public Proxy(SchoolGirl mm)
        {
            gg = new Pursuit (mm);
        }
        public void GiveDolls()
        {
            gg.GiveDolls ();
        }
        public void GiveFlowers()
        {
            gg.GiveFlowers ();
        }
        public void GiveChocolate()
        {
            gg.GiveChocolate ();
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            SchoolGirl jj = new SchoolGirl();
            jj.Name = "jioajioa";
            Proxy daili = new Proxy (jj);
            daili.GiveDolls ();
            daili.GiveChocolate ();
            daili.GiveFlowers ();
            Console.Read ();
        }
    }
}

代理模式应用

上一篇下一篇

猜你喜欢

热点阅读