Meet the observers and observables in C#


This article introduces the usage of two special C# classes, Observable and Observer class. I first meet these features in Java for a first time, in their generic class collection.

Observable is a base class for making new class observable by the Observer child class.  Observer objects can watch what is going on in the above-mentioned objects.

The Book Example

I have a class for holding Book information[Book] and had another class for dealing the Books, Book Manager. The Board class will push notification of new titles. Let’s explore how these objects communicate.

Here BookManager is being observed by the Board object, in other words, BookManager is the child of Observable and Board is from the Observer.

Here is the code[Book.cs]

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObserverExample
{
 class Book
 {

private string mtitle;
 public Book(string title)
 {
 this.mtitle = title;
 }
 public string GetTtitle
 {
 get { return this.mtitle; }
 }
 }
class BookManager : IObservable<Book>
 {
 private List<IObserver<Book>> observers;
 private List<Book> blist; 
 public BookManager()
 {
 observers = new List<IObserver<Book>>();
 Console.WriteLine("I am book manager");
 blist = new List<Book>();
 }
 public void NewArrivals(Book b)
 {
 blist.Add(b);
 }
 public IDisposable Subscribe(IObserver<Book> observer)
 {
 // Check whether observer is already registered. If not, add it
 if (!observers.Contains(observer))
 {
 observers.Add(observer);
 Console.WriteLine("Subscribed Book manager");
 
 foreach (Book n in blist)
 {
 observer.OnNext(n);
 }
 }
 return new Unsubscriber<Book>(observers, observer);
 }
 }




class Board : IObserver<Book>
 {
 private IDisposable newarrivals;
 private string name;
 public Board(string mname)
 {
 this.name = mname;
 Console.WriteLine("I am the Book Board!");
 }
 public virtual void Subscribe(BookManager provider)
 {
 newarrivals = provider.Subscribe(this);
 Console.WriteLine("Subscribed the Board @");
 }

public virtual void Unsubscribe()
 {
 newarrivals.Dispose();
 }

public virtual void OnCompleted()
 {
 Console.WriteLine("On complete " + this);
 }
 public virtual void OnNext(Book info)
 {
 Console.WriteLine("On board:" + info.GetTtitle);
 }

// No implementation needed: Method is not called by the BaggageHandler class.
 public virtual void OnError(Exception e)
 {
 // No implementation.
 }
 }

internal class Unsubscriber<Book> : IDisposable
 {
 private List<IObserver<Book>> _observers;
 private IObserver<Book> _observer;

internal Unsubscriber(List<IObserver<Book>> observers, IObserver<Book> observer)
 {
 this._observers = observers;
 this._observer = observer;
 }

public void Dispose()
 {
 if (_observers.Contains(_observer))
 _observers.Remove(_observer);
 }
 }

}

The OutPut

2018-05-27_090632

There are plenty of applications using the technique, such online store notification of available stocks, etc.

That’s what I found useful today.

You can find the source code at @  Github

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.