Skip to content

Observer Pattern

at-grigorievich edited this page Feb 7, 2022 · 1 revision

Observer - behavioral design pattern.

Uses a one-to-many relationship. In this regard, there is one suspicious object and many observers. And when an allergy to an object occurs, an alert to all observers automatically occurs.

C# already has a built-in implementation of the pattern using interfaces:IObservable<T> & IObserver<T>

  1. Сlass that implements an interface IObservable<T> is called a provider or subject.
  2. Сlass that implements the interface IObserver<T> is called an observer that receives notifications from the provider.
  3. IObserver<T>.OnNext method that provides the observer with new or current information

Example:

Press LMB key to press button and call everyone observers to move to the center.

  1. All observers move to their points :

  1. The button is pressed, all observers received a notification and came to the center:

CODE

Clone this wiki locally