Observer Design Pattern C#

The observer design pattern enables a subscriber to register with and receive notifications from a provider.

Example: There is a product(iPhone6 – Provider) and some customers are interested(subscribers) in buying this product. but they wish to wait for the product price to reduce. so, they will subscribe to the provider and receive update on price changes.

UML:

640px-Observer.svg
By WikiSolvedOwn work, Public Domain, Link

namespace TestObserverPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            //Products
            Product iPhone6 = new Product("iPhone6", 65000);
            Product sumsungGalaxyNote5 = new Product("SamsungGalaxyNote5", 48000);

            //Customers interested in iPhone6
            iPhone6.Attach(new InterestedCustomer("Shiva"));
            iPhone6.Attach(new InterestedCustomer("Basava"));

            //Customers interested in SamsungGalaxyNote5
            sumsungGalaxyNote5.Attach(new InterestedCustomer("Ganesh"));
            sumsungGalaxyNote5.Attach(new InterestedCustomer("Rohini"));

            //Procuct price changes (will be notify to the customer)
            iPhone6.Price = 55000;
            sumsungGalaxyNote5.Price = 38000;
            iPhone6.Price = 45000;
            sumsungGalaxyNote5.Price = 28000;

            Console.ReadKey();
        }
    }

    /// <summary>
    /// Subject
    /// </summary>

    public class Product
    {
        private string _productName;
        private double _productPrice;       

        List<IInterestedCustomer> _customers = new List<IInterestedCustomer>();

        public Product(string productName, double productPrice)
        {
            this._productName = productName;
            this._productPrice = productPrice;
        }

        //Attach Customer
        public void Attach(IInterestedCustomer customer)
        {
            _customers.Add(customer);
        }

        //Detach Customer
        public void Detach(IInterestedCustomer customer)
        {
            _customers.Remove(customer);
        }

        //Notify Customer
        public void Notify()
        {
            foreach (IInterestedCustomer customer in _customers)
            {
                customer.InformInterestedCustomer(this);
            }
            Console.WriteLine("---");
        }

        //Set Price (this will cal notify)
        public double Price
        {
            get { return _productPrice; }
            set
            {
                if (_productPrice != value)
                {
                    _productPrice = value;
                    Notify();
                }
            }
        }

        //Get ProductName
        public string ProductName
        {
            get { return _productName; }
        }
    }

    /// <summary>
    /// IObserver
    /// </summary>

    public interface IInterestedCustomer
    {
        void InformInterestedCustomer(Product stock);
    }

    /// <summary>
    /// Observer
    /// </summary>

    public class InterestedCustomer : IInterestedCustomer
    {
        private string _interestedCustomerName;        

        public InterestedCustomer(string interestedCustomerName)
        {
            this._interestedCustomerName = interestedCustomerName;
        }

        public void InformInterestedCustomer(Product stock)
        {
            Console.WriteLine("Notified Customer {0}: {1} price changed to {2}", _interestedCustomerName, stock.ProductName, stock.Price);
        }
    }
}

Output:
ObserverPatternOutput

Hope, The above code and output is self explanatory 🙂

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s