Observer Pattern

noun

say it: “ub-ZER-ver PAT-ern” /əbˈzɜːrvər ˈpætərn/

A design pattern where subscribers are told automatically whenever something they watch changes.

What is Observer Pattern?

The Observer pattern lets objects subscribe to another object and be told when something happens. A YouTube channel notifies all its subscribers when a new video goes up.

public class Channel
{
    public event Action<string>? VideoPublished;
    public void Publish(string title) => VideoPublished?.Invoke(title);
}

channel.VideoPublished += title => Console.WriteLine(quot;New video: {title}");

In a sentence

  • “Observables follow the Observer pattern: subscribers hear about each new value.”

Level: Freelancer · More OOP terms at this level

Updated