Last active
April 5, 2020 18:12
-
-
Save smkplus/6fef0686fdd36cdd6bb464bbaf24f54b to your computer and use it in GitHub Desktop.
Observer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
//Invokes the notificaton method | |
public class Subject : MonoBehaviour | |
{ | |
//A list with observers that are waiting for something to happen | |
public List<Observer> observers = new List<Observer>(); | |
private void Start() | |
{ | |
Notify(); | |
} | |
//Send notifications if something has happened | |
public void Notify() | |
{ | |
foreach (var observer in observers) | |
{ | |
//Notify all observers even though some may not be interested in what has happened | |
//Each observer should check if it is interested in this event | |
observer.OnNotify(); | |
} | |
} | |
//Add observer to the list | |
public void AddObserver(Observer observer) | |
{ | |
observers.Add(observer); | |
} | |
//Remove observer from the list | |
public void RemoveObserver(Observer observer) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment