Unity implements design pattern - observer pattern

Unity implements design pattern - observer pattern

The observer design pattern defines a one-to-many combination relationship between objects, so that when the state of an object changes, all objects that depend on it are notified and automatically refreshed.
To put it simply, someone who needs to receive a notification only needs to subscribe to the notification. When the notification is sent, it will be sent to everyone who subscribes to the notification.
Insert image description here
Next, use the stock changes to notify people who subscribe to this stock.

1.IInvestor(‘Observer’ interface)

Investor interface

    interface IInvestor
    {
    
    
        void Update(Stock stock);
    }

2.Investor(‘ConcreteObserver’ class)

Investors concretely realize

    class Investor : IInvestor
    {
    
    
        private string _name;
        private Stock _stock;

        // Constructor
        public Investor(string name)
        {
    
    
            this._name = name;
        }

        public void Update(Stock stock)
        {
    
    
            //Debug.Log("Notified {0} of {1}'s " +"change to {2:C}", _name, stock.Symbol, stock.Price);
            Debug.Log("Notified "+ _name+" of "+ stock+"'s " + "change to "+stock.Price);
        }

        // Gets or sets the stock
        public Stock Stock
        {
    
    
            get {
    
     return _stock; }
            set {
    
     _stock = value; }
        }
    }

3.Stock(The ‘Subject’ abstract class)

Stock base class

abstract class Stock
    {
    
    
        private string _symbol;
        private double _price;
        private List<IInvestor> _investors = new List<IInvestor>();

        // Constructor
        public Stock(string symbol, double price)
        {
    
    
            this._symbol = symbol;
            this._price = price;
        }

        public void Attach(IInvestor investor)
        {
    
    
            _investors.Add(investor);
        }

        public void Detach(IInvestor investor)
        {
    
    
            _investors.Remove(investor);
        }

        public void Notify()
        {
    
    
            foreach (IInvestor investor in _investors)
            {
    
    
                investor.Update(this);
            }

            Debug.Log("Stock Notify( ) called");
        }

        // Gets or sets the price
        public double Price
        {
    
    
            get {
    
     return _price; }
            set
            {
    
    
                if (_price != value)
                {
    
    
                    _price = value;
                    Notify();
                }
            }
        }

        // Gets the symbol
        public string Symbol
        {
    
    
            get {
    
     return _symbol; }
        }
    }

4.IBM(‘ConcreteSubject’ class)

'IBM' the stock

    class IBM : Stock
    {
    
    
        // Constructor
        public IBM(string symbol, double price)
          : base(symbol, price)
        {
    
    
        }
    }

5. Test

    public class ObserverExample1 : MonoBehaviour
    {
    
    
        void Start()
        {
    
    
            // Create IBM stock and attach investors
            IBM ibm = new IBM("IBM", 120.00);
            ibm.Attach(new Investor("Sorros"));
            ibm.Attach(new Investor("Berkshire"));

            // Fluctuating prices will notify investors
            ibm.Price = 120.10;
            ibm.Price = 121.00;
            ibm.Price = 120.50;
            ibm.Price = 120.75;

        }
    }

Guess you like

Origin blog.csdn.net/zzzsss123333/article/details/133340595