zoukankan      html  css  js  c++  java
  • 设计模式-代理模式

    通过代理完成对业务类的访问,包一层方便人以功能扩展。

    代理模式:VPN代理,翻墙代理,火车票代理等。

    通过代理业务类去完成对真实业务类的调用,代理类不能扩展业务功能,在不修改RealSubject前提下,插入功能。

    包一层:没有什么技术问题是包一层解决不了的,如果有,那么就再包一层。比如来个日志记录,可以避免修改业务类,只需要修改代理类。在来个异常处理;在来个性能提成----缓存结果........

    通过代理,能够为对象扩展功能(不是增加业务)而不去修改原始业务类,也就是包了一层,我的地盘听我的

     /// <summary>
     /// 业务接口
     /// </summary>
     public interface ISubject
     {
         /// <summary>
         /// get
         /// </summary>
         /// <returns></returns>
         bool GetSomething();
    
         /// <summary>
         /// do
         /// </summary>
         void DoSomething();
     }
    public class ProxySubject : ISubject
    {
        //组合一下
        private static ISubject _Subject = new RealSubject();
        public void DoSomething()
        {
            try
            {
                Console.WriteLine("prepare DoSomething...");
                _Subject.DoSomething();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
    
        private static Dictionary<string, bool> ProxyDictionary = new Dictionary<string, bool>();
        public bool GetSomething()
        {
            try
            {
                Console.WriteLine("prepare GetSomething...");
                string key = "Proxy_GetSomething";
                bool bResult = false;
                if (!ProxyDictionary.ContainsKey(key))
                {
                    bResult = _Subject.GetSomething();
                    ProxyDictionary.Add(key, bResult);
                }
                else
                {
                    bResult = ProxyDictionary[key];
                }
                return bResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
    }
    View Code
    /// <summary>
    /// 一个耗时耗资源的对象方法
    /// 一个第三方封装的类和方法
    /// </summary>
    public class RealSubject : ISubject
    {
        public RealSubject()
        {
            Thread.Sleep(2000);
            long lResult = 0;
            for (int i = 0; i < 100000000; i++)
            {
                lResult += i;
            }
            Console.WriteLine("RealSubject被构造。。。");
        }
    
        /// <summary>
        /// 火车站查询火车票
        /// </summary>
        public bool GetSomething()
        {
            Console.WriteLine("坐车去火车站看看余票信息。。。");
            Thread.Sleep(3000);
            Console.WriteLine("到火车站,看到是有票的");
            return true;
        }
    
        /// <summary>
        /// 火车站买票
        /// </summary>
        public void DoSomething()
        {
            Console.WriteLine("开始排队。。。");
            Thread.Sleep(2000);
            Console.WriteLine("终于买到票了。。。");
        }
    }
    View Code

    调用一下:

     {
         Console.WriteLine("***********Real**************");
    
         ISubject subject = new RealSubject();
         subject.GetSomething();
         //subject.DoSomething();
     }
     {
         Console.WriteLine("***********Proxy**************");
         ISubject subject = new ProxySubject();
         subject.GetSomething();
         //subject.DoSomething();
     }
     {
         Console.WriteLine("***********Proxy**************");
         ISubject subject = new ProxySubject();
         subject.GetSomething();
         //subject.DoSomething();
     }
    View Code

    调用下看下结果:

     通过代理,能够为对象扩展功能(不是增加业务),而不是去修改原始业务类,就是包一层。

    WebService就是代理模式。

  • 相关阅读:
    Retrofit2.0+OkHttp设置统一的请求头(request headers)
    Retrofit、Okhttp使用小记(cookie,accesstoken,POST
    quartz持久化部署实现
    支付宝支付-常用支付API详解(查询、退款、提现等)
    支付宝支付-提现到个人支付宝
    Git 版本还原命令
    CEF JS实现获取剪贴板图片的DataURL
    CEF 自定义用户协议(scheme)实现以二进制流的方式显示图片、视频、音频
    CEF C++调用前端js方法展示传递过来的图片数据
    C++读写图片数据转成Base64格式
  • 原文地址:https://www.cnblogs.com/taotaozhuanyong/p/11552254.html
Copyright © 2011-2022 走看看