C# 设计模式(十一)代理模式

1.为什么使用代理模式

在某些不能或者不方便直接调用对象时候,代理对象就是访问者和对象之间的中介。

2.代理模式的实现

实现要素:

  • 抽象主题
  • 具体主题
  • 代理对象

代码实现

/// <summary>
    /// 抽象主题
    /// </summary>
    public interface ISubject
    {
        void DoSomething();

        List<string> GetSomething();
    }
    
    /// <summary>
    /// 具体主题
    /// </summary>
    public class RealSubject : ISubject
    {
        public void DoSomething()
        {
            Console.WriteLine("执行DoSomething方法");
        }

        public List<string> GetSomething()
        {
            Thread.Sleep(3000);
            List<string> result = new List<string>()
            {
                "111",
                "222",
                "333"
            };
            return result;
        }
    }
    
    /// <summary>
    /// 单例代理
    /// </summary>
    public class SingletonProxy : AbstractProxy
    {
        private static ISubject subject = new RealSubject();
        public SingletonProxy()
        {
        }

        public override void DoSomething()
        {
            subject.DoSomething();
        }

        public override List<string> GetSomething()
        {
            return subject.GetSomething();
        }
    }
    
    /// <summary>
    /// 异常代理
    /// </summary>
    public class ExceptionProxy : ISubject
    {
        private ISubject subject = new RealSubject();

        public void DoSomething()
        {
            try
            {
                subject.DoSomething();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public List<string> GetSomething()
        {
            try
            {
                return subject.GetSomething();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }
    }
    
    /// <summary>
    /// 缓存代理
    /// </summary>
    public class CacheProxy : ISubject
    {
        private ISubject subject = new RealSubject();
        public void DoSomething()
        {
            subject.DoSomething();
        }

        public List<string> GetSomething()
        {
            string key = nameof(CacheProxy) + "_" + nameof(GetSomething);
            if (CacheHelper.Exists(key))
            {
                return CacheHelper.Get<List<string>>(key);
            }
            else
            {
                List<string> result = subject.GetSomething();
                CacheHelper.Add(key, result);
                return result;
            }
        }
    }
    
    /// <summary>
    /// 缓存帮助类
    /// </summary>
    public class CacheHelper
    {
        private static Dictionary<string, object> _DicCache = new Dictionary<string, object>();

        public static void Add(string key, object value)
        {
            _DicCache.Add(key, value);
        }

        public static T Get<T>(string key)
        {
            return (T)_DicCache[key];
        }

        public static bool Exists(string key)
        {
            return _DicCache.ContainsKey(key);
        }
    }

 

以上便是代理模式的实现

3.总结一下

优点:上层与低层分离,一定程度上减少耦合、对目标对象的扩展、

缺点:增加了系统复杂度

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值