設計模式--介面卡模式/代理模式

你好呀嗯嗯發表於2024-08-28

1.介面卡模式:把一個類適配到原有的介面上,可以組合 可以繼承

一.繼承的方式:

class RedisHelperInhert : RedisHelper, IHelper
    {
        public void Add<T>()
        {
            base.AddRedis<T>();
        }

        public void Delete<T>()
        {
            base.DeleteRedis<T>();
        }

        public void Query<T>()
        {
            base.QueryRedis<T>();
        }

        public void Update<T>()
        {
            base.UpdateRedis<T>();
        }
    }

二.組合的方式:

class RedisHelperCombination : IHelper
    {
        /// <summary>
        /// 1.欄位屬性方式組合   預設構造  特別強烈,而且寫死了
        /// </summary>
        private RedisHelper _RedisHelper = new RedisHelper();

        private RedisHelper _RedisHelperCtor = null;
        /// <summary>
        /// 2.建構函式方式組合 ,例項化會一定傳入,但是物件可以選擇
        /// </summary>
        /// <param name="redisHelper"></param>
        public RedisHelperCombination(RedisHelper redisHelper)
        {
            this._RedisHelper = redisHelper;
        }
        public RedisHelperCombination()
        {
            
        }
        /// <summary>
        /// 3.方法方式組合  物件可以選擇,而且可有可無
        /// </summary>
        /// <param name="redisHelper"></param>
        public void AddRedisHelper(RedisHelper redisHelper)
        {
            this._RedisHelperCtor = redisHelper;
        }

        public void Add<T>()
        {
            _RedisHelper.AddRedis<T>();
        }

        public void Delete<T>()
        {
            _RedisHelper.DeleteRedis<T>();
        }

        public void Query<T>()
        {
            _RedisHelper.QueryRedis<T>();
        }

        public void Update<T>()
        {
            _RedisHelper.UpdateRedis<T>();
        }
    }

呼叫如下:

    Console.WriteLine("**************");
                {
                    IHelper helper = new RedisHelperInhert();
                    helper.Add<Program>();
                    helper.Delete<Program>();
                    helper.Query<Program>();
                    helper.Update<Program>();
                }

                Console.WriteLine("**************");
                {
                    IHelper helper = new RedisHelperCombination();
                    helper.Add<Program>();
                    helper.Delete<Program>();
                    helper.Query<Program>();
                    helper.Update<Program>();
                }

2.代理模式:透過代理類來訪問業務類,在不修改業務類的前提下可以擴充套件功能

原有類:

class RealSubject:ISubject
    {
        public RealSubject()
        {
            Thread.Sleep(2000);
            long lResult = 0;
            for(int i = 0; i < 1000000; i++)
            {
                lResult += i;
            }
            Console.WriteLine("RealSubject被構造。。。");
        }
        public bool GetSomething()
        {
            Console.WriteLine("坐車去火車站看看餘票資訊");
            Thread.Sleep(3000);
            Console.WriteLine("到火車站,看到是有票的");
            return true;
        }
        public void DoSomething()
        {
            Console.WriteLine("開始排隊。。。");
            Thread.Sleep(2000);
            Console.WriteLine("終於買到票了。。。");
        }
    }
/// <summary>
    /// 代理:只能傳達原有邏輯,不能新增業務邏輯
    /// 包一層:
    /// 加日誌:修改代理類完成   日誌代理  避免對業務類修改
    /// 單例: 修改代理類完成,搞個static(並不是強制單例,因為其他地方也可以new) 避免對業務類修改
    /// 快取:修改代理類完成,加個快取邏輯  避免對業務類修改
    /// </summary>
    public class ProxySubject:ISubject
    {
        private ISubject _iSubject = new RealSubject();

        //private static ISubject _iSubject = new RealSubject();  單例的例子

        //private static Dictionary<string, bool> ProxySubjectDictionary = new Dictionary<string, bool>(); //快取的例子

        //public bool GetSomething()
        //{
        //    Console.WriteLine("before GetSomething");
        //    bool _BooleanResult = false;
        //    string key = "GetSomething";
        //    if (ProxySubjectDictionary.ContainsKey(key))
        //    {
        //        _BooleanResult = ProxySubjectDictionary[key];
        //    }
        //    else
        //    {
        //        _BooleanResult = _iSubject.GetSomething();
        //        ProxySubjectDictionary.Add(key, _BooleanResult);
        //    }
        //    Console.WriteLine("after GetSomething");
        //    return _BooleanResult;
        //}

        /// <summary>
        /// 火車站查詢火車票
        /// </summary>
        /// <returns></returns>
        public bool GetSomething()
        {
            Console.WriteLine("before GetSomething");
            bool _BooleanResult = _iSubject.GetSomething();
            Console.WriteLine("after GetSomething");
            return _BooleanResult;
        }

        public void DoSomething()
        {
            Console.WriteLine("before DoSomething");
            _iSubject.DoSomething();
            Console.WriteLine("after Dosomething");
        }
    }

呼叫如下:

static void Main(string[] args)
        {
            {
                ISubject subject = new RealSubject();
                subject.GetSomething();
                subject.DoSomething();
            }
            {
                ISubject subject = new ProxySubject();
                subject.GetSomething();
                subject.DoSomething();
            }
        }

相關文章