Observer Examples:

Java
Java
Python
Python
PHP
PHP
C#
C#
C++
C++
TypeScript
TypeScript
▸ Observer Quick Review

Observer Pattern in Java

Observer:

   public class Observer
   {
       // The data:
       // Helper for a linked list structure:
       public Observer next;

       // Constructor:
       public Observer()
       {
           next = null;
       }

       // The update method, called to by the Subject class.
       public void Update(String message)
       {
           System.out.println(message);
       }

   }

Subject:

   public class Subject
   {
       // The root of the subscribed objects in the linked-list structure:
       Observer root;

       public Subject()
       {
           root = null;
       }

       // Add an observer to the list:
       // You can also feel free to use a built-in structure like a List<T> instead!
       public void AddObserver(Observer observer)
       {
           // Insert head-first into the list.
           observer.next = root;
           root = observer;
       }

       public void Notify(String message)
       {
           // Iterate the observers, notifying them all:
           Observer tmp = this.root;
           while(tmp != null)
           {
               tmp.Update(message);
               tmp = tmp.next;
           }
       }

   }

Find any bugs in the code? let us know!