.net设计模式 (外观模式)学习笔记

运用设计模式只是为了解决一类问题的,当解决掉当前一类问题,通常会在解决这个问题时候 带来其他问题  合理应用扬长避短


结构性设计模式:关注的是类与类之间的关系

                          .net设计模式 (外观模式)学习笔记  

概要:实现复杂业务逻辑的转移包装。 上端 不需要直接和复杂逻辑 进行交互, 交给 中间者(第三者)  处理。分层结构处理

优点:对复杂逻辑的依赖,对象的管理 转移到 FacadeCenter(外观模式)中去。上层使用也更简洁。

现在有一个业务需求:用户需要多登陆检测,然后 检测库存, 物流 然后才下单

    /// <summary>
    /// 登录接口
    /// </summary>
    public interface  ILogisticeSystem
    {
        bool CheckLogistice(int productId,int cityId);
        void NewLogistice(int productId, int cityId);
    }

    /// <summary>
    /// 登录检查
    /// </summary>
    class LogisticeSystem : ILogisticeSystem
    {

        public bool CheckLogistice(int productId, int cityId)
        {
            //繁琐检查
            return true;
        }

        public void NewLogistice(int productId, int cityId)
        {
             
        }
    }



    /// <summary>
    /// 订单系统
    /// </summary>
    public interface IOrderSystem
    {
        bool CheckOrder(int productId);
    }

    /// <summary>
    /// 订单检查
    /// </summary>
    public class OrderSystem : IOrderSystem
    {
        public bool CheckOrder(int productId)
        {
            return true;
        }
    }

    public interface IStrorageSystem
    {
        bool CheckStrorage(int productId,int cityId);
    }

    /// <summary>
    /// 仓库检查
    /// </summary>
    public class StrorageSystem : IStrorageSystem
    {
        public bool CheckStrorage(int productId, int cityId)
        {
           return true;
        }
    }



    public interface IUserSystem
    {
        bool CheckUser(int id);
    }

    /// <summary>
    /// 用户检查
    /// </summary>
    public class UserSystem : IUserSystem
    {
        public bool CheckUser(int id)
        {
            return true;
        }
    }





    /// <summary>
    /// 外观模式, 转移来的具体逻辑
    /// </summary>
    public class FacadeCenter
    {
        private int id = 99;
        private int productId = 999;
        private int cityId = 9999;
        public FacadeCenter(int id, int productId ,int cityId)
        {
            this.id         = id;
            this.productId  = productId;
            this.cityId     = cityId;
        }

        public void MakeOrber()
        {
            IUserSystem userSystem = new UserSystem();
            IStrorageSystem strorageSystem = new StrorageSystem();
            IOrderSystem orderSystem = new OrderSystem();
            ILogisticeSystem logisticeSystem = new LogisticeSystem();


            if (!userSystem.CheckUser(id))
            {
                Console.WriteLine("检测失败.");
            }
            else if (!strorageSystem.CheckStrorage(productId, cityId))
            {
                Console.WriteLine("检测失败.");
            }
            else if (!orderSystem.CheckOrder(id))
            {
                Console.WriteLine("检测失败.");
            }
            else if (!logisticeSystem.CheckLogistice(productId, cityId))
            {
                Console.WriteLine("检测失败.");
            }
            else
            {
                Console.WriteLine("开始下单");
            }
        }

    }





    class Program
    {
        static void Main(string[] args)
        {
            int id          = 99;
            int productId   = 999;
            int cityId      = 9999;

            ////////////////////////////////////////////////////////////////////////////////
            ///////////////////////  普通模式                ///////////////////////////////
            {

                IUserSystem userSystem = new UserSystem();
                IStrorageSystem strorageSystem = new StrorageSystem();
                IOrderSystem orderSystem = new OrderSystem();
                ILogisticeSystem logisticeSystem = new LogisticeSystem();


                if (!userSystem.CheckUser(id))
                {
                    Console.WriteLine("检测失败.");
                }
                else if (!strorageSystem.CheckStrorage(productId, cityId))
                {
                    Console.WriteLine("检测失败.");
                }
                else if (!orderSystem.CheckOrder(id))
                {
                    Console.WriteLine("检测失败.");
                }
                else if (!logisticeSystem.CheckLogistice(productId, cityId))
                {
                    Console.WriteLine("检测失败.");
                }
                else
                {
                    Console.WriteLine("开始下单");
                }
            }
            ///////////////////////  普通模式                ///////////////////////////////
            ////////////////////////////////////////////////////////////////////////////////


            /////外观模式测出现
            FacadeCenter facadeCenter = new FacadeCenter(id, productId, cityId);///一般我们会把 外观模式设置成 单例模式
            facadeCenter.MakeOrber();

        }
    }




猜你喜欢

转载自blog.csdn.net/nicepainkiller/article/details/83988624
今日推荐