Unity はデザインパターン - オブザーバーパターンを実装します

Unity はデザインパターン - オブザーバーパターンを実装します

オブザーバー デザイン パターンは、オブジェクト間の 1 対多の組み合わせ関係を定義するため、オブジェクトの状態が変化すると、それに依存するすべてのオブジェクトが通知され、自動的に更新されます。
簡単に言うと、通知を受信する必要がある人は通知を購読するだけでよく、通知が送信されると、通知を購読している全員に通知が送信されます。
ここに画像の説明を挿入します
次に、株式の変更を使用して、この株式を購読しているユーザーに通知します。

1.IInvestor(「オブザーバー」インターフェース)

投資家インターフェース

    interface IInvestor
    {
    
    
        void Update(Stock stock);
    }

2.投資家(「ConcreteObserver」クラス)

投資家は具体的に気づく

    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(「Subject」抽象クラス)

ストック基本クラス

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'クラス)

「IBM」株

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

5. テスト

    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;

        }
    }

おすすめ

転載: blog.csdn.net/zzzsss123333/article/details/133340595
おすすめ