C#设计模式之---观察者模式

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

    观察者模式Observer Pattern

    观察者模式Observer Pattern是一种对象行为模式。它定义对象间的一种一对多的依赖关系当一个对象的状态发生改变时所有依赖于它的对象都得到通知并被自动更新。在观察者模式中主体是通知的发布者它发出通知时并不需要知道谁是它的观察者可以有任意数目的观察者订阅并接收通知。观察者模式不仅被广泛应用于软件界面元素之间的交互在业务对象之间的交互、权限管理等方面也有广泛的应用。观察者模式的主要的作用就是对对象解耦将观察者和被观察者完全隔离。

    1面向对象方式实现

    using System;
    using System.Collections.Generic;
    namespace ConsoleApplication
    {
        //一般每个接口或类都写在单独的.cs文件中
        //本示例为了执行查看方便才写在一起  
        /// 
        public interface IObserver
        {
            void Action();
        }
        public class Baby : IObserver
        {
            public void Action()
            {
                this.Cry();
            }
            public void Cry()
            {
                Console.WriteLine("{0} Cry", this.GetType().Name);
            }
        }
        public class Brother : IObserver
        {
            public void Action()
            {
                this.Turn();
            }
            public void Turn()
            {
                Console.WriteLine("{0} Turn", this.GetType().Name);
            }
        }
        public class Chicken : IObserver
        {
            public void Action()
            {
                this.Woo();
            }
            public void Woo()
            {
                Console.WriteLine("{0} Woo", this.GetType().Name);
            }
        }
        public class Dog : IObserver
        {
            public void Action()
            {
                this.Wang();
            }
            public void Wang()
            {
                Console.WriteLine("{0} Wang", this.GetType().Name);
            }
        }
        public class Neighbor : IObserver
        {
            public void Action()
            {
                this.Awake();
            }
            public void Awake()
            {
                Console.WriteLine("{0} Awake", this.GetType().Name);
            }
        }
        public class Animal
        {
            //一般的实现
            //public void Sound()
            //{
            //    Console.WriteLine("{0} Sound.....", this.GetType().Name);
            //    new Chicken().Woo();
            //    new Baby().Cry();
            //    new Brother().Turn();
            //    new Dog().Wang();
            //    new Neighbor().Awake();
            //}
            private List<IObserver> _ObserverList = new List<IObserver>();
            public void Add(IObserver observer)
            {
                this._ObserverList.Add(observer);
            }
            public void Remove(IObserver observer)
            {
                this._ObserverList.Remove(observer);
            }
            public void SoundObserver()
            {
                Console.WriteLine("{0} SoundObserver.....", this.GetType().Name);
                foreach (var observer in this._ObserverList)
                {
                    observer.Action();
                }
            }
        }
        // 
        /// 观察者模式
        /// 对象和行为的分离
        /// 
        class Program
        {
            static void Main(string[] args)
            {
                Animal animal = new Animal();
                animal.Add(new Baby());
                animal.Add(new Brother());
                animal.Add(new Chicken());
                animal.Add(new Dog());
                animal.Add(new Neighbor());
                animal.SoundObserver();
            }
        }
    }
    

     2事件委托方式实现

    using System;
    using System.Collections.Generic;
    namespace ConsoleApplianimalion
    {
        //一般每个接口或类都写在单独的.cs文件中
        //本示例为了执行查看方便才写在一起  
        public interface IObserver
        {
            void Action();
        }
        public class Baby : IObserver
        {
            public void Action()
            {
                this.Cry();
            }
            public void Cry()
            {
                Console.WriteLine("{0} Cry", this.GetType().Name);
            }
        }
        public class Brother : IObserver
        {
            public void Action()
            {
                this.Turn();
            }
            public void Turn()
            {
                Console.WriteLine("{0} Turn", this.GetType().Name);
            }
        }
        public class Chicken : IObserver
        {
            public void Action()
            {
                this.Woo();
            }
            public void Woo()
            {
                Console.WriteLine("{0} Woo", this.GetType().Name);
            }
        }
        public class Dog : IObserver
        {
            public void Action()
            {
                this.Wang();
            }
            public void Wang()
            {
                Console.WriteLine("{0} Wang", this.GetType().Name);
            }
        }
        public class Neighbor : IObserver
        {
            public void Action()
            {
                this.Awake();
            }
            public void Awake()
            {
                Console.WriteLine("{0} Awake", this.GetType().Name);
            }
        }
        public class Animal
        {
            //一般的实现
            //public void Sound()
            //{
            //    Console.WriteLine("{0} Sound.....", this.GetType().Name);
            //    new Chicken().Woo();
            //    new Baby().Cry();
            //    new Brother().Turn();
            //    new Dog().Wang();
            //    new Neighbor().Awake();
            //}
            public event Action SoundHandler;
            public void SoundEvent()
            {
                Console.WriteLine("{0} SoundEvent.....", this.GetType().Name);
                if (this.SoundHandler != null)
                {
                    //foreach (Action action in this.SoundHandler.GetInvoanimalionList())
                    //{
                    //    action.Invoke();
                    //}
                    this.SoundHandler.Invoke();
                }
            }
        }
        // 
        /// 观察者模式
        /// 对象和行为的分离
        /// 
        class Program
        {
            static void Main(string[] args)
            {
                Animal animal = new Animal();
                animal.SoundHandler += new Action(() => new Dog().Wang());
                animal.SoundHandler += new Chicken().Woo;
                animal.SoundHandler += new Baby().Cry;
                animal.SoundHandler += new Brother().Turn;
                animal.SoundHandler += new Neighbor().Awake;
                animal.SoundEvent();
            }
        }
    }

     

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

    “C#设计模式之---观察者模式” 的相关文章

    C#常用加密解密方法(MD5加密解密)

    在日常开发过程中总会遇到需要加密解密的需求这里我整理了C#常用的加密解密方法分享给大家。 先看看加密的基本概念 "加密"是一种限制对网络上传输数据的访问权的技术。原始数据也称为明文plaintext)被加密设备(硬件或软件)和密钥加密而产生的经过编码的数据称为密文ciphe...

    实体首部:Content-Location

    首部字段Content-Location给出与报文主体部分相对应的URI.和首部字段Location不同,Content-Location表示的是报文主体返回资源对应的URI. 比如,对于使用首部字段Accept-Language的服务器驱动型请求,当返回的...

    【Python】OpenCV读取视频帧并保存为图片

    文章目录 cv2.VideoCapture()读取视频帧计算FPS的两种方法读取视频帧并保存为图片Reference cv2.VideoCapture()读取视频帧 import cv2 # 代入OpenCV模块 VIDEO_PATH = 'video.mp4&...

    C++ STL入门教程(6)——set(集合)的使用(附完整程序代码)

    一、简介集合(Set)是一种包含已排序对象的关联容器,不允许有重复元素。 begin()返回指向第一个元素的迭代器clear()清除所有元素count()返回某个值元素的个数empty()如果集合为空,返回trueend()返回指向最后一个元素的迭代器equal_range()返回集合中与给定值相等...

    CP vs SP

    1.什么是SP?SP是英文Service Provider的缩写,中文翻译为服务提供商,通常是指在移动网内运营增值业务的社会合作单位。它们建立与移动网络建立相连的服务平台,为手机用户提供一系列信息服务,如:娱乐、游戏、短信、彩信、WAP、彩铃、铃声下载、定位等等。 2.什么是CP?CP是英...

    商务智能

    一 1、什么是商务智能产生的驱动力 c a将企业内部的数据转换为利润 b商务智能可帮助企业收集信息 c商务智能可将数据转换为信息将信息转换为知识进而支持企业进行决策 d商务智能也制定企业决策 2、智能化企业具有快速吸收新想法的能力、适应新情况的能力有效解决问题的能力、调用适当资源的能力、有效积累...