Ad Code

Observer Pattern and Data Plotting in Database

We use observer Pattern in database and data plotting;  In the class diagram of observer pattern we made two pure virtual classes and then inherited concrete objects from it for implementation; why we initially defined pure virtual classes at the first place....why can't we declare and define concrete classes directly and use them in implementing this pattern?



It seems to me that may have seen some code of Observer Design pattern written in C++ where pure virtual functional may have been used to create classes. In C++, we use use pure virtual functions in a class to make the class as abstract class. Abstract classes provide a powerful design technique which promotes code re-use and polymorphism. Now, given below is the complete code for the object model provided in the handouts.

But the below given code is written in C# language. As in the code, you can see that the classes Subject and Observer are abstract classes. It means that new classes will be inherited from these abstract classes using class inheritance. The purpose of using inheritance is to encourage code reuse. As we have studied that the purpose of Observer Pattern is that these patterns can be used when multiple displays of state are needed. It means that there can be more than one ConcreteObserver classes for any system. All these ConcreteObservers will be inherited from the class Observer using inheritance therefore the class Observer was made as abstract class.

// Observer pattern -- Structural example


using System;
using System.Collections.Generic;

namespace DoFactory.GangOfFour.Observer.Structural
{
  /// <summary>
  /// MainApp startup class for Structural
  /// Observer Design Pattern.
  /// </summary>
  class MainApp
  {
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    static void Main()
    {
      // Configure Observer pattern
      ConcreteSubject s = new ConcreteSubject();

      s.Attach(new ConcreteObserver(s, "X"));
      s.Attach(new ConcreteObserver(s, "Y"));
      s.Attach(new ConcreteObserver(s, "Z"));

      // Change subject and notify observers
      s.SubjectState = "ABC";
      s.Notify();

      // Wait for user
      Console.ReadKey();
    }
  }

  /// <summary>
  /// The 'Subject' abstract class
  /// </summary>
  abstract class Subject
  {
    private List<Observer> _observers = new List<Observer>();

    public void Attach(Observer observer)
    {
      _observers.Add(observer);
    }

    public void Detach(Observer observer)
    {
      _observers.Remove(observer);
    }

    public void Notify()
    {
      foreach (Observer o in _observers)
      {
        o.Update();
      }
    }
  }

  /// <summary>
  /// The 'ConcreteSubject' class
  /// </summary>
  class ConcreteSubject : Subject
  {
    private string _subjectState;

    // Gets or sets subject state
    public string SubjectState
    {
      get { return _subjectState; }
      set { _subjectState = value; }
    }
  }

  /// <summary>
  /// The 'Observer' abstract class
  /// </summary>
  abstract class Observer
  {
    public abstract void Update();
  }

  /// <summary>
  /// The 'ConcreteObserver' class
  /// </summary>
  class ConcreteObserver : Observer
  {
    private string _name;
    private string _observerState;
    private ConcreteSubject _subject;

    // Constructor
    public ConcreteObserver(
      ConcreteSubject subject, string name)
    {
      this._subject = subject;
      this._name = name;
    }

    public override void Update()
    {
      _observerState = _subject.SubjectState;
      Console.WriteLine("Observer {0}'s new state is {1}",
        _name, _observerState);
    }

    // Gets or sets subject
    public ConcreteSubject Subject
    {
      get { return _subject; }
      set { _subject = value; }
    }
  }
}

Hopefully, now it is clear to you.
Reactions

Post a Comment

0 Comments