boxmoe_header_banner_img

Hello! 欢迎来到我的博客!

加载中

文章导读

设计模式之观察者模式


avatar
xiaoifei 2024年3月17日 1.14k

设计模式概览

  • 创建型模式
    • 抽象工厂模式(Abstract Factory)
    • 建造者模式(Builder)
    • 工厂方法模式(Factory Method)
    • 原型模式(Prototype)
    • 单例模式(Singleton)
  • 结构型模式
    • 适配器模式(Adapter)
    • 桥接模式(Bridge)
    • 组合模式(Composite)
    • 装饰模式(Decorator)
    • 外观模式(Facade)
    • 享元模式(Flyweight)
    • 代理模式(Proxy)
  • 行为型模式
    • 职责链模式(Chain of Responsibility)
    • 命令模式(Command)
    • 解释器模式(Interpreter)
    • 迭代器模式(Iterator)
    • 中介者模式(Mediator)
    • 备忘录模式(Memento)
    • 观察者模式(Observer)
    • 状态模式(State)
    • 策略模式(Strategy)
    • 模板方法模式(Template Method)
    • 访问者模式(Visitor)

观察者模式

例如:一个按钮相当于被观察者,而数据管理器,音乐,相当于观察者,我们点击按钮就会触发观察者不同行为

案例:猫捉老鼠

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

namespace 观察者模式
{
    public class Cat
    {
        public string name;
        public void CatComing(Mouse mouse1, Mouse mouse2, Mouse mouse3)
        {
            Console.WriteLine($"{name} is coming");
            mouse1.MouseRun();
            mouse2.MouseRun();
            mouse3.MouseRun();
        }
    }

    public class Mouse
    {
        public string name;
        public void MouseRun() 
        {
            Console.WriteLine($"{name} is Running Away");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat() { name = "Tom" };
            Mouse mouse1 = new Mouse() {name = "Jerry1" };
            Mouse mouse2 = new Mouse() { name = "Jerry2" };
            Mouse mouse3 = new Mouse() { name = "Jerry3" };
            cat.CatComing(mouse1, mouse2, mouse3);

        }
    }
}

这段代码的意思是:当猫来了,老鼠逃跑
但是实现是将老鼠传入猫的方法当中,一旦老鼠一多,代码就变得冗长且难以维护
所以在这里我们将猫来了这个函数作为一个消息,这个消息会发送给老鼠

此时我们将被观察者建立一个委托,将观察者的逃跑方法加入的这个委托,只要被观察者一来就调用这个委托,委托的内容就是观察者逃跑
同样有类比:按钮是被观察者,当他被点击的时候就会调用这个委托,而这个委托的内容实际上是其他类(例如声音类用来调整音量的方法),只要按钮一被点击,就会静音

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

namespace 观察者模式
{
    public class Cat
    {
        public string name;
        public Action Listener;
        public void CatComing()
        {
            Console.WriteLine($"{name} is coming");
            Listener?.Invoke();
        }
    }

    public class Mouse
    {
        public string name;
        public void MouseRun() 
        {
            Console.WriteLine($"{name} is Running Away");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat() { name = "Tom" };
            Mouse mouse1 = new Mouse() {name = "Jerry1" };
            Mouse mouse2 = new Mouse() { name = "Jerry2" };
            Mouse mouse3 = new Mouse() { name = "Jerry3" };
            //注册
            cat.Listener += mouse1.MouseRun;
            cat.Listener += mouse2.MouseRun;
            cat.Listener += mouse3.MouseRun;
            cat.CatComing();
        }
    }
}

在观察者将自己的方法传入委托的过程就叫做注册
用事件来优化,保证事件不能在类外触发
public event Action Listener;

*被观察者*声明事件的过程叫做**发布消息**,而*观察者*将自身方法注册进被观察者的过程叫**订阅消息**



评论(0)

查看评论列表

暂无评论


发表评论

表情 颜文字
插入代码