Haxe Code Cookbook
Haxe programming cookbookDesign patternsObserver


Reading time: 0.5 minute

This is a basic example of the Observer design pattern in Haxe. The pattern makes use of an Observer interface and an Observable base class to notify objects when another object's property is changed so that they can react accordingly.

interface Observer {
  public function notified(sender:Observable, ?data:Any) : Void;

class Observable {
  private var observers:Array<Observer> = [];
  public function new() { }
  private function notify<T>(?data:T) {
    for(obs in observers)
      obs.notified(this, data);
  public function addObserver(observer:Observer) {



  • Extra care has to be put into making sure that an observable cannot register the same observer twice.
  • The fact that Observable is a class can make it hard to use because Haxe does not allow for multiple inheritance. Instead, we used a static extension for convenient usage.

Last modified:
Category:  Design patterns