美文网首页
[Unity C#]观察者模式

[Unity C#]观察者模式

作者: Loyal_HAC | 来源:发表于2020-05-28 14:37 被阅读0次

    概念

    观察者模式(有时又被称为模型(Model)-视图(View)模式、源-收听者(Listener)模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实现事件处理系统。
    顾名思义观察者模式,肯定有观察者,既然有观察者就会有被观察的对象,观察者可以有多个,当被观察的对象状态改变的时候每个观察者会做出不同的响应,举个栗子,在召唤师峡谷正在进行一场团战,一个ad正在被自己的辅助和对面的刺客ob着,突然ad被不知道从哪里的技能打残了,这个时候ob者ad的辅助肯定会放技能保护adc,对面的刺客就会入场切这个ad

    模块

    主题(Subject):将有关状态存入具体观察者对象;在具体主题内部状态改变时,给所有登记过的观察者发出通知。
    抽象观察者(Observer):为所有的具体观察者定义一个接口,在得到主题通知时更新自己。
    具体观察者(ConcreteObserver):实现抽象观察者角色所要求的更新接口,以便使本身的状态与主题状态协调。

    来个示例

    我们用林克来举例吧,在《塞尔达荒野之息》中,每个怪物据点都有站在哨塔的波克布林,这个波克布林就相当于主题,他会给据点里面的其他怪物提示是否有敌人(偷瞄林克大魔王是否过来),据点里面的怪物就是不同的观察者,会更据主题提供的被观察对象的状态来改变状态

    开撸代码

    我们先写个抽象的观察者类,里面有构造函数和更新状态的接口

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /// <summary>
    /// 观察者抽象类
    /// </summary>
    public abstract class Observe {
        protected string weapon;
        protected string name;
        public Observe(string weapon,string name)
        {
            this.weapon = weapon;
            this.name = name;
        }
        /// <summary>
        /// 的到通知后的响应
        /// </summary>
        public abstract void Responce();    
    }
    

    然后再写具体观察者类,例如波克布林,蜥蜴怪,人马

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PigMonster : Observe
    {
    
        public PigMonster(string weapon, string name) : base(weapon, name)
        {
            this.weapon = weapon;
            this.name = name;
        }
        public override void Responce()
        {
            Debug.Log(name + "拿起了" + weapon + "然后他就朝着林克A了上去,最后林克大魔王被迫反杀了他");
        }
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class LizardsWarrior : Observe
    {
        public LizardsWarrior (string weapon,string name) : base(weapon, name)
        {
            this.weapon = weapon;
            this.name = name;
        }
        public override void Responce()
        {
            Debug.Log(name + "拿起了" + weapon + "然后他就朝着林克A了上去,最后林克大魔王被迫反杀了他");
        }
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    public class Sagittarius : Observe {
    
        public Sagittarius(string weapon,string name) : base(weapon, name)
        {
            this.weapon = weapon;
            this.name = name;
        }
        public override void Responce()
        {
            Debug.Log(name + "拿起了" + weapon + "然后他就朝着林克A了上去,最后林克大魔王被迫反杀了他");
        }
        
    }
    

    然后我们写个脚本去添加、删除观察者并通知各个观察者被观察对象的状态也就是主题的代码

    using System.Collections;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using UnityEngine;
    /// <summary>
    /// 主题  发布消息的人或者被观察者
    /// </summary>
    public class Subject{
        public delegate void Issue();
        public Issue issue;
        public void AddObserve(Issue Observe)
        {
          issue += Observe;
        }
        public void RemoveObserve(Issue Observe)
        {
            issue -= Observe;
        }
    
        public void Publish()
        {
            Debug.Log("前方高台上的哨兵看到林克走了过来,他吹响了哨子");
            issue();
        }
    }
    

    最后我们来测试下

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class TestObserve : MonoBehaviour {
    
        // Use this for initialization
        void Start () {
            Subject subject = new Subject();
            PigMonster pigMonster = new PigMonster("旅人之剑", "红色的波克布林");
            PigMonster pigMonster1 = new PigMonster("波克棒", "白银波克布林");
            LizardsWarrior lizardsWarrior = new LizardsWarrior("蜥蜴弓", "黄色蜥蜴战士");
            LizardsWarrior lizardsWarrior1 = new LizardsWarrior("强力蜥蜴飞旋镖", "红色蜥蜴战士");
            Sagittarius sagittarius = new Sagittarius("麦辣鸡腿", "白银人马");
    
            subject.AddObserve(pigMonster.Responce);
            subject.AddObserve(pigMonster1.Responce);
            subject.AddObserve(lizardsWarrior.Responce);
            subject.AddObserve(lizardsWarrior1.Responce);
            subject.AddObserve(sagittarius.Responce);
            subject.Publish();
        }
    }
    

    运行结果

    运行结果

    相关文章

      网友评论

          本文标题:[Unity C#]观察者模式

          本文链接:https://www.haomeiwen.com/subject/qtrgahtx.html