国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C#設計模式之---觀察者模式

這篇具有很好參考價值的文章主要介紹了C#設計模式之---觀察者模式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

觀察者模式(Observer Pattern)

觀察者模式(Observer Pattern)是一種對象行為模式。它定義對象間的一種一對多的依賴關系,當一個對象的狀態(tài)發(fā)生改變時,所有依賴于它的對象都得到通知并被自動更新。在觀察者模式中,主體是通知的發(fā)布者,它發(fā)出通知時并不需要知道誰是它的觀察者,可以有任意數(shù)目的觀察者訂閱并接收通知。觀察者模式不僅被廣泛應用于軟件界面元素之間的交互,在業(yè)務對象之間的交互、權限管理等方面也有廣泛的應用。觀察者模式的主要的作用就是對對象解耦,將觀察者和被觀察者完全隔離。

1)面向對象方式實現(xiàn)

using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
    //一般每個接口或類都寫在單獨的.cs文件中
    //本示例為了執(zhí)行查看方便才寫在一起  
    /// 
    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
    {
        //一般的實現(xiàn)
        //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)事件委托方式實現(xiàn)

using System;
using System.Collections.Generic;
namespace ConsoleApplianimalion
{
    //一般每個接口或類都寫在單獨的.cs文件中
    //本示例為了執(zhí)行查看方便才寫在一起  
    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
    {
        //一般的實現(xiàn)
        //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();
        }
    }
}

?文章來源地址http://www.zghlxwxcb.cn/news/detail-562361.html

到了這里,關于C#設計模式之---觀察者模式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【設計模式】觀察者模式

    【設計模式】觀察者模式

    觀察者模式(又被稱為發(fā)布-訂閱(Publish/Subscribe)模式,屬于行為型模式的一種,它定義了一種一對多的依賴關系,讓多個觀察者對象同時監(jiān)聽某一個主題對象。這個主題對象在狀態(tài)變化時,會通知所有的觀察者對象,使他們能夠自動更新自己。 Subject:抽象主題(被觀察者

    2024年02月13日
    瀏覽(20)
  • 設計模式---觀察者模式

    1,概念 ????????屬于行為模式的一種,定義了一種一對多的依賴關系,讓多個觀察者對象同時監(jiān)聽某一對象主題對象,這個主題對象在狀態(tài)變化時,會通知所有的觀察者對象,使他們能夠自動更新自己。 在觀察者模式中有如下角色: Subject:抽象主題(抽象被觀察者),

    2024年02月15日
    瀏覽(18)
  • 設計模式:觀察者模式

    定義 觀察者模式(Observer Pattern)是一種行為設計模式,允許一個對象(稱為“主題”或“可觀察對象”)維護一組依賴于它的對象(稱為“觀察者”),當主題的狀態(tài)發(fā)生變化時,會自動通知所有觀察者對象。 應用場景 觀察者模式適用于以下場景: 聯(lián)動反應 :當一個對象

    2024年04月08日
    瀏覽(24)
  • 設計模式-觀察者模式

    觀察者模式是一種行為型設計模式,它定義了一種一對多的依賴關系,當一個對象的狀態(tài)發(fā)生改變時,其所有依賴者都會收到通知并自動更新。當對象間存在一對多關系時,則使用觀察者模式(Observer Pattern)。比如,當一個對象被修改時,則會自動通知依賴它的對象。觀察者

    2024年02月15日
    瀏覽(23)
  • 設計模式——14. 觀察者模式

    設計模式——14. 觀察者模式

    觀察者模式(Observer Pattern)是一種行為型設計模式,用于定義對象之間的一對多依賴關系,使得當一個對象的狀態(tài)發(fā)生改變時,所有依賴于它的對象都能夠自動收到通知并更新自己的狀態(tài),以保持與被觀察對象的同步。觀察者模式也被稱為發(fā)布-訂閱模式。 觀察者模式包含以

    2024年02月07日
    瀏覽(20)
  • 設計模式(11)觀察者模式

    設計模式(11)觀察者模式

    一、概述: 1、定義:觀察者模式定義了一種一對多的依賴關系,讓多個觀察者對象同時監(jiān)聽某一個主題對象。這個主題對象在狀態(tài)發(fā)生變化時,會通知所有觀察者對象,使它們能夠自動更新自己。 2、結構圖: 實現(xiàn) ?調用

    2024年02月11日
    瀏覽(30)
  • 設計模式之觀察者模式

    可以幫你的對象知悉現(xiàn)況,不會錯過該對象感興趣的事。對象甚至在運行時可決定是否要繼續(xù)被通知。 從報紙和雜志的訂閱說起: 報社的業(yè)務就是出版報紙 向某家報社訂閱報紙,只要他們有新報紙出版,就會給你送來。只要你是他們的訂戶,你就會一直收到新報紙。 當你不

    2024年01月24日
    瀏覽(24)
  • 重溫設計模式 --- 觀察者模式

    觀察者模式 是一種行為型設計模式,它允許對象之間建立一種一對多的關系,使得當一個對象狀態(tài)改變時,所有依賴它的對象都能夠自動得到通知并更新自己的狀態(tài)。該模式可以幫助我們實現(xiàn)松耦合的系統(tǒng),以便更好地應對變化和擴展。 在觀察者模式中,有兩個角色: 觀察

    2024年02月13日
    瀏覽(22)
  • 觀察者設計模式

    觀察者設計模式

    行為型模式(Behavioral Patterns):這類模式主要關注對象之間的通信。它們 分別是: 職責鏈模式(Chain of Responsibility) 命令模式(Command) 解釋器模式(Interpreter) 迭代器模式(Iterator) 中介者模式(Mediator) 備忘錄模式(Memento) 觀察者模式(Observer) 狀態(tài)模式(State) 策略

    2024年01月24日
    瀏覽(25)
  • 設計模式-觀察者

    設計模式-觀察者

    觀察者模式是一種廣泛應用于軟件開發(fā)中的行為設計模式,尤其是在面向對象編程(OOP)中。該模式定義了對象之間的一對多依賴關系,當一個對象的狀態(tài)發(fā)生改變時,所有依賴于它的對象都會得到通知并自動更新 在觀察者模式中,存在兩個主要角色: 主題(Subject) 或 被

    2024年01月22日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包