首頁 > 軟體

一起來學習C#的觀察者模式

2022-03-23 13:01:22
using System;
using System.Collections.Generic;
public class Example
{
    public static void Main()
    {
        BaggageHandler provider = new BaggageHandler();
        ArrivalsMonitor observer1 = new ArrivalsMonitor("BaggageClaimMonitor1");
        ArrivalsMonitor observer2 = new ArrivalsMonitor("SecurityExit");
        // 新增資訊,此時沒有觀察者,對於新的資訊不會進行操作,若有觀察者,則對觀察者內部資料進行對比,若有新內容則進行更新。
        provider.BaggageStatus(712, "Detroit", 3);
        // 新增了觀察者,觀察者內部也有資料集。新資訊傳進來,會與內部資料集進行對比,若有新內容則進行更新。
        observer1.Subscribe(provider);
        provider.BaggageStatus(712, "Kalamazoo", 3);
        provider.BaggageStatus(400, "New York-Kennedy", 1);
        provider.BaggageStatus(712, "Detroit", 3);
        observer2.Subscribe(provider);
        provider.BaggageStatus(511, "San Francisco", 2);
        //引數3為0 執行移除712對應資訊值,對應觀察者列印
        provider.BaggageStatus(712);
        observer2.Unsubscribe();
        provider.BaggageStatus(400);
        provider.LastBaggageClaimed();
    }
}
// The example displays the following output:
//      Arrivals information from BaggageClaimMonitor1
//      Detroit                712    3
//
//      Arrivals information from BaggageClaimMonitor1
//      Detroit                712    3
//      Kalamazoo              712    3
//
//      Arrivals information from BaggageClaimMonitor1
//      Detroit                712    3
//      Kalamazoo              712    3
//      New York-Kennedy       400    1
//
//      Arrivals information from SecurityExit
//      Detroit                712    3
//
//      Arrivals information from SecurityExit
//      Detroit                712    3
//      Kalamazoo              712    3
//
//      Arrivals information from SecurityExit
//      Detroit                712    3
//      Kalamazoo              712    3
//      New York-Kennedy       400    1
//
//      Arrivals information from BaggageClaimMonitor1
//      Detroit                712    3
//      Kalamazoo              712    3
//      New York-Kennedy       400    1
//      San Francisco          511    2
//
//      Arrivals information from SecurityExit
//      Detroit                712    3
//      Kalamazoo              712    3
//      New York-Kennedy       400    1
//      San Francisco          511    2
//
//      Arrivals information from BaggageClaimMonitor1
//      New York-Kennedy       400    1
//      San Francisco          511    2
//
//      Arrivals information from SecurityExit
//      New York-Kennedy       400    1
//      San Francisco          511    2
//
//      Arrivals information from BaggageClaimMonitor1
//      San Francisco          511    2
using System;
using System.Collections.Generic;
public class ArrivalsMonitor : IObserver<BaggageInfo>
{
    private string name;
    private List<string> flightInfos = new List<string>();
    private IDisposable cancellation;
    private string fmt = "{0,-20} {1,5}  {2, 3}";
    public ArrivalsMonitor(string name)
    {
        if (String.IsNullOrEmpty(name))
            throw new ArgumentNullException("The observer must be assigned a name.");
        this.name = name;
    }
    public virtual void Subscribe(BaggageHandler provider)
    {
                      // 引數是處理者! 新增觀察者 this
        cancellation = provider.Subscribe(this);
    }
    public virtual void Unsubscribe()
    {
        cancellation.Dispose();
        flightInfos.Clear();
    }
    public virtual void OnCompleted()
    {
        flightInfos.Clear();
    }
    // No implementation needed: Method is not called by the BaggageHandler class.
    public virtual void OnError(Exception e)
    {
        // No implementation.
    }
    // Update information.
    public virtual void OnNext(BaggageInfo info)
    {
        bool updated = false;
        // Flight has unloaded its baggage; remove from the monitor.
        if (info.Carousel == 0)
        {
            var flightsToRemove = new List<string>();
            string flightNo = String.Format("{0,5}", info.FlightNumber);
            foreach (var flightInfo in flightInfos)
            {
                if (flightInfo.Substring(21, 5).Equals(flightNo))
                {
                    flightsToRemove.Add(flightInfo);
                    updated = true;
                }
            }
            foreach (var flightToRemove in flightsToRemove)
                flightInfos.Remove(flightToRemove);
            flightsToRemove.Clear();
        }
        else
        {
            // Add flight if it does not exist in the collection.
            string flightInfo = String.Format(fmt, info.From, info.FlightNumber, info.Carousel);
            if (!flightInfos.Contains(flightInfo))
            {
                flightInfos.Add(flightInfo);
                updated = true;
            }
        }
        if (updated)
        {
            flightInfos.Sort();
            Console.WriteLine("Arrivals information from {0}", this.name);
            foreach (var flightInfo in flightInfos)
                Console.WriteLine(flightInfo);
            Console.WriteLine();
        }
    }
}
using System;
using System.Collections.Generic;
public class BaggageHandler : IObservable<BaggageInfo>
{
    private List<IObserver<BaggageInfo>> observers;
    private List<BaggageInfo> flights;
    public BaggageHandler()
    {
        observers = new List<IObserver<BaggageInfo>>();
        flights = new List<BaggageInfo>();
    }
    public IDisposable Subscribe(IObserver<BaggageInfo> observer)
    {
        // Check whether observer is already registered. If not, add it
        if (!observers.Contains(observer))
        {
            observers.Add(observer);
            // Provide observer with existing data.
            foreach (var item in flights)
                observer.OnNext(item);
        }
        return new Unsubscriber<BaggageInfo>(observers, observer);
    }
    // Called to indicate all baggage is now unloaded.
    public void BaggageStatus(int flightNo)
    {
        BaggageStatus(flightNo, String.Empty, 0);
    }
    public void BaggageStatus(int flightNo, string from, int carousel)
    {
        var info = new BaggageInfo(flightNo, from, carousel);
        // Carousel is assigned, so add new info object to list.
        if (carousel > 0 && !flights.Contains(info))
        {
            flights.Add(info);
            foreach (var observer in observers)
                observer.OnNext(info);
        }
        else if (carousel == 0)
        {
            // Baggage claim for flight is done
            var flightsToRemove = new List<BaggageInfo>();
            foreach (var flight in flights)
            {
                if (info.FlightNumber == flight.FlightNumber)
                {
                    flightsToRemove.Add(flight);
                    foreach (var observer in observers)
                        observer.OnNext(info);
                }
            }
            foreach (var flightToRemove in flightsToRemove)
                flights.Remove(flightToRemove);
            flightsToRemove.Clear();
        }
    }
    public void LastBaggageClaimed()
    {
        foreach (var observer in observers)
            observer.OnCompleted();
        observers.Clear();
    }
}
using System;
using System.Collections.Generic;
public class BaggageInfo
{
    private int flightNo;
    private string origin;
    private int location;
    internal BaggageInfo(int flight, string from, int carousel)
    {
        this.flightNo = flight;
        this.origin = from;
        this.location = carousel;
    }
    public int FlightNumber
    {
        get { return this.flightNo; }
    }
    public string From
    {
        get { return this.origin; }
    }
    public int Carousel
    {
        get { return this.location; }
    }
}
using System;
using System.Collections.Generic;
internal class Unsubscriber<BaggageInfo> : IDisposable
{
    private List<IObserver<BaggageInfo>> _observers;
    private IObserver<BaggageInfo> _observer;
    internal Unsubscriber(List<IObserver<BaggageInfo>> observers, IObserver<BaggageInfo> observer)
    {
        this._observers = observers;
        this._observer = observer;
    }
    public void Dispose()
    {
        if (_observers.Contains(_observer))
            _observers.Remove(_observer);
    }
}

流程簡單分析

個人理解

類:

  • 核心處理類(控制者/處理者)
    • 資訊資料列表
    • 觀察者列表
  • 觀察者類
  • 資料資訊類
  • 釋放空間

流程:

處理者接收新資料判斷是否存在,不存在新增到資料列表中,然後遍歷觀察者列表,根據新資料執行新動作。

觀察者類主要實現自身的新增和刪除操作,將處理者作為引數傳進自身中,呼叫處理者中的新增觀察者函數進行新增繫結。

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!     


IT145.com E-mail:sddin#qq.com