Skip to content

A lightweight generic Observer Pattern solution

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

Cosmo7/Observable

Repository files navigation

Observable

A lightweight generic Observer Pattern solution.

What

This is a simple implementation of Observer Pattern in the form of a generic wrapper class. Observer Pattern essentially means that instead of continually polling another object for changes you are setting a callback that is invoked whenever the observed object changes.

Why

To be honest I found the Microsoft IObserver and IObservable approach a bit too complicated. I've tried various ways of doing Observer Pattern and this is the one that involves the least amount of code smell.

How

Suppose we have a typical situation where client class needs to update when a global static value changes. The normal way would be to regularly update the client, which polls the value and compares it to the previous value.

Like this:

public class ApplicationState
{
  public static string message;
  
  private void SomeFunction()
  {
    message = "Something";
  }
}

public class Client
{
  private string oldMessage;
  
  public void Update()
  {
    // poll ApplicationState.message and see if it has changed
    if(oldMessage != ApplicationState.message)
    {
      OnMessageChanged(ApplicationState.message);
      oldMessage = ApplicationState.message;
    }
  }
  
  private void OnMessageChange(string newMessageValue)
  {
    // do the necessary
  }
}

With Observable, we would wrap the global static value and then bind a callback to it, like this:

public class ApplicationState
{
  public static Observable<string> message = new Observable<string>();
  
  private void SomeFunction()
  {
    message.value = "Something";
  }
}

public class Client
{
  public Client() // constructor
  {
    ApplicationState.message.Bind(OnMessageChange);
  }

  private void OnMessageChange(string newMessageValue)
  {
    // do the necessary
  }
}

Besides being easier to read, the Observable version doesn't require continually updating the client object or tracking and comparing values.

Limitations

There's no fancy threading or error handling; this is basically just a wrapper around System.Action with no checking that observers still exist.

About

A lightweight generic Observer Pattern solution

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages