zoukankan      html  css  js  c++  java
  • 《大话设计模式》学习笔记4:代理模式

      

      

    代追求者送礼物示例:

      

    1.Subject:

        public interface IGiveGift
        {
            void GiveDolls();
            void GiveFlowers();
            void GiveChocolate();
        }

    2.RealSubject:

        public class Pursuit:IGiveGift
        {
            SchoolGril girl;
            public Pursuit(SchoolGril girl)
            {
                this.girl = girl;
            }
            public void GiveDolls()
            {
                Console.WriteLine(girl.Name + ",送你洋娃娃");
            }
    
            public void GiveFlowers()
            {
                Console.WriteLine(girl.Name + ",送你鲜花");
            }
    
            public void GiveChocolate()
            {
                Console.WriteLine(girl.Name + ",送你巧克力");
            }
        }

    3.Proxy:

        public class Proxy:IGiveGift
        {
            Pursuit pursuit;
            public Proxy(SchoolGril girl)
            {
                pursuit = new Pursuit(girl);
            }
            public void GiveDolls()
            {
                pursuit.GiveDolls();
            }
    
            public void GiveFlowers()
            {
                pursuit.GiveFlowers();
            }
    
            public void GiveChocolate()
            {
                pursuit.GiveChocolate();
            }
        }

    4.其他类及客户端代码:

        public class SchoolGril
        {
            public string Name { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                SchoolGril girl = new SchoolGril();
                girl.Name = "美女";
                Proxy proxy = new Proxy(girl);
                proxy.GiveDolls();
                proxy.GiveFlowers();
                proxy.GiveChocolate();
            }
        }

    代理模式的应用:

    1.远程代理,也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间的事实。如WebServie。

    2.虚拟代理,是根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象。

    3.安全代理,用来控制真实对象访问时的权限。

    4.智能指引,是指当调用真实的对象时,代理处理另外一些事情。

  • 相关阅读:
    xUtils 中的BitmapUtils 全面注释
    321影音代码
    android studio使用技巧
    java android面试题分析总结
    android面试题分析总结
    据说年薪30万的Android程序员必须知道的帖子
    好用软件
    win10找回win7的windows照片查看器
    github上传代码
    android 常见错误集锦
  • 原文地址:https://www.cnblogs.com/walden1024/p/4487871.html
Copyright © 2011-2022 走看看