扎实基础_设计模式_行为型_观察者模式(项目实战,使用委托注册事情,消除多重判断)

1:最开始需求 在我们的系统中有一个数据在新增之后要发送给RA系统

  于是代码1.0:就写成一个公共方法,在数据新增的地方调用

 2:突然代码又来了一个删除的需求 于是

 3: 现在需求又来了 需要推送系统可配置,想推那个系统就推那个系统, 代码如下:

using ObserverPattern.Observer;
using ObserverPattern.Subject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
        }

        public void ADDdata()
        {//自己的系统 在新增数据的时候 在新增方法里面 把数据推送给别的系统
            push push = new push();
            push.Update("项目ID");
        }

        public void deleteDate()
        {//自己的系统 在删除数据的时候 在新增方法里面 把数据推送给别的系统
            push push = new push();
            push.Delete("项目ID");
        }
    }
    //推送公共类
    public class push
    {
        //Update方法
        public void Update(string id)
        {
            Console.WriteLine("update数据给RI");
        }

        //delete方法
        public void Delete(string id)
        {

            Console.WriteLine("delete数据给RI");
        }

        public void Commm(string id, string pushState)
        {
            //<add key="push" value="1"> 0:推RO,2 推KA ,3 不推 4 推KA RO
            string state = "";//根据配置文件获取需要推送的系统 
            switch (state)
            {
                case "0":
                    switch (pushState)//根据状态判断是update还是delete
                    {
                        case "update": UpdateRO(); break;

                        case "Delte": DeleteRO(); break;

                    }

                    break;
                case "1":
                    switch (pushState)//根据状态判断是update还是delete
                    {
                        case "update": UpdateKA(); break;

                        case "Delte": DeleteKA(); break;

                    }
                    break;

                case "3": break;

                case "4":

                    switch (pushState)//根据状态判断是update还是delete
                    {
                        case "update":
                            UpdateRO();
                            UpdateKA(); 
                            break;

                        case "Delte":
                            DeleteKA();
                            DeleteRO();
                            break;

                    }
                    break;
             
            }

        }

        public void UpdateKA()
        {
            Console.WriteLine("推送数据给KA");
        }

        public void DeleteKA()
        {

            Console.WriteLine("推送删除数据给KA");
        }


        public void UpdateRO()
        {
            Console.WriteLine("推送数据给RO");
        }

        public void DeleteRO()
        {
            Console.WriteLine("推送删除数据给RO");

        }
    }


}

4:现在需求又增加了!,又要新增加一个系统的推送, 而且还需要配置推送不同的系统 按照上面的代码, 如果到了四个系统,这代码是绝对写不下去的 

 5:开始使用我们的 观察者模式+委托 消除垃圾代码  代码2.0正式上线

 首先面向对象   把 RO OA KA 封装为三个类 (观察者) 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern.Observer
{
    public class KA 
    {
        public  void updatePush()
        {
            Console.WriteLine("推送数据给KA");
         
        }

        public void deltePush()
        {
            Console.WriteLine("推送数据给KA");

        }
    }
}

 第一步:因为 RO KA OA 都有公共的update delete方法  并且有可能还需要实现一些公共方法,比如插入日志啊之类的 于是增加抽象类(抽象观察者)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern
{
    public abstract class IObserver
    {
        public abstract void updatePush();

        public virtual void insertData()
        {
            Console.WriteLine("增加虚方法,给多个系统使用,插入日志啊,插入失败数据到数据库好重新请求呀之类的");
        }
    }
}

第二步:然后  RO KA OA继承 Ioberver   KA OA写法一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern.Observer
{
    public class RO : IObserver
    {
        public override void updatePush()
        {
            Console.WriteLine("推送数据给RO");
            //调用共同的公共插入日志方法
            base.insertData();
        }
    }
}

第三步:增加主题, 

using ObserverPattern.Observer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern.Subject
{
    public class push
    {
        public event Action updateEvent;
        public void update()
        {
            if (updateEvent != null)
            {
                updateEvent.Invoke();
            }
        }


        public event Action DeleteEvent;
        public void Delete()
        {
            if (DeleteEvent != null)
            {
                DeleteEvent.Invoke();
            }
        }
    }
  
   
}

第四步:如何调用

using ObserverPattern.Observer;
using ObserverPattern.Subject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
        }

        public void ADDdata()
        {//自己的系统 在新增数据的时候 在新增方法里面 把数据推送给别的系统
            push push = new push();
            push.Update("项目ID");
        }

        public void deleteDate()
        {//自己的系统 在删除数据的时候 在新增方法里面 把数据推送给别的系统
            push push = new push();
            push.Delete("项目ID");
        }
    }
    //推送公共类
    public class push
    {
        //Update方法
        public void Update(string id)
        {
            #region 2.0 使用观察者模式加委托 消除判断

            string KA = "1";//<add key="KA" value="1"> 0:推送 1:关闭
            string OA = "1";//<add key="OA" value="1"> 0:推送 1:关闭
            string RO = "1";//<add key="RO" value="1"> 0:推送 1:关闭

            pushSystem push = new pushSystem();
            if (RO == "1")
            { 
               push.updateEvent += new RO().updatePush;
            }
            if (KA == "1")
            {
                push.updateEvent +=()=> new KA().updatePush("ID");
            }
            #endregion

            Console.WriteLine("update数据给RI");
        }

        //delete方法
        public void Delete(string id)
        {
            #region 2.0 使用观察者模式加委托 消除判断

            string KA = "1";//<add key="KA" value="1"> 0:推送 1:关闭
            string OA = "1";//<add key="OA" value="1"> 0:推送 1:关闭
            string RO = "1";//<add key="RO" value="1"> 0:推送 1:关闭

            pushSystem push = new pushSystem();
            if (RO == "1")
            {
                push.updateEvent += new RO().deltePush;
            }
            if (KA == "1")
            {
                push.updateEvent += () => new KA().deltePush(123);
            }
            #endregion

            Console.WriteLine("update数据给RI");
            Console.WriteLine("delete数据给RI");
        }
    }


}

观察者模式示意图: 并且与步骤3对比一下

猜你喜欢

转载自www.cnblogs.com/LZXX/p/12975970.html