大话设计模式-代理模式

Posted 如若

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大话设计模式-代理模式相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    interface IGiveGift
    {
        void GiveDolls();
        void GiveFlowers();
        void GvieChoolate();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    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 GvieChoolate()
        {
            gg.GvieChoolate();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    abstract class Subject
    {
        public abstract void Request();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    class Pursuit:IGiveGift
    {
        SchoolGirl mm;

        public Pursuit(SchoolGirl mm) 
        {
            this.mm = mm;
        }

        public void GiveDolls()
        {
            Console.WriteLine("{0}送你洋娃娃!",mm.Name);
        }

        public void GiveFlowers()
        {
            Console.WriteLine("{0}送你鲜花!",mm.Name);
        }

        public void GvieChoolate()
        {
            Console.WriteLine("{0}送你巧克力!",mm.Name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    class RealSubject:Subject
    {
        public override void Request()
        {
            Console.WriteLine("真实的请求!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    class SubProxy:Subject
    {
        RealSubject realSubject;

        public override void Request()
        {
            if (realSubject == null)
            {
                realSubject = new RealSubject();
            }
            realSubject.Request();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    class SchoolGirl
    {
        private string name;

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

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProxyFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            SchoolGirl mm = new SchoolGirl();
            mm.Name = "李娇娇";

            Proxy daili = new Proxy(mm);
            daili.GiveDolls();
            daili.GiveFlowers();
            daili.GvieChoolate();


            SubProxy subProxy = new SubProxy();
            subProxy.Request();

            Console.ReadLine();
        }
    }
}

 代理模式

  代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.

 

以上是关于大话设计模式-代理模式的主要内容,如果未能解决你的问题,请参考以下文章

大话设计模式-代理模式

大话设计-代理模式

大话涉及模式Python实现-代理模式

大话设计模式读书笔记——代理模式

java 之 代理模式(大话设计模式)

《大话设计模式》——代理模式