Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active April 5, 2020 18:12
Show Gist options
  • Save smkplus/6fef0686fdd36cdd6bb464bbaf24f54b to your computer and use it in GitHub Desktop.
Save smkplus/6fef0686fdd36cdd6bb464bbaf24f54b to your computer and use it in GitHub Desktop.
Observer
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