结构性设计模式-代理模式(Proxy)

1.原有的代码

      /// <summary>
    /// 业务接口
    /// </summary>
    public interface ISubject
    {
        /// <summary>
        /// get
        /// </summary>
        /// <returns></returns>
        bool GetSomething();

        /// <summary>
        /// do
        /// </summary>
        void DoSomething();
    }
  /// <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("This is DoSomething Before");
            //try
            //{


            Console.WriteLine("开始排队。。。");
            Thread.Sleep(2000);
            Console.WriteLine("终于买到票了。。。");
            //}
            //catch (Exception)
            //{

            //    throw;
            //}
        }
    }

2.矛盾的产生

业务需求的变更需要在原来基础上增加新的逻辑,但是原本的业务已经足够复杂不想破话原来的结构,比如缓存代理

3.解决

    /// <summary>
    /// 第三方缓存 能在内存中差常驻数据  且能获取
    /// </summary>
    public class CustomCache
    {
        private static Dictionary<string, object> CustomCacheDictionary = new Dictionary<string, object>
            ();
        public static void Add(string key, object value)
        {
            CustomCacheDictionary.Add(key, value);
        }
        public static T Get<T>(string key)
        {
            return (T)CustomCacheDictionary[key];
        }
        public static bool Exist(string key)
        {
            return CustomCacheDictionary.ContainsKey(key);
        }
    }
   public class ProxySubject : ISubject
    {
        private static ISubject _iSubject = new RealSubject();
        public void DoSomething()
        {
            try
            {
                Console.WriteLine("This is DoSomething Before");
                _iSubject.DoSomething();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        public bool GetSomething()
        {
            string key = $"{nameof(ProxySubject)}_{nameof(GetSomething)}";
            bool bResult = false;
            if (CustomCache.Exist(key))
            {
                bResult = CustomCache.Get<bool>(key);
            }
            else
            {
               bResult= _iSubject.GetSomething();
                CustomCache.Add(key, bResult);
            }

            return bResult;
            //return _iSubject.GetSomething();
        }


    }

4.总结

命名约束:XXXCacheProxy

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值