Click here to Skip to main content
15,893,668 members
Articles / Web Development / ASP.NET

Implementing the Observer Pattern

Rate me:
Please Sign up or sign in to vote.
3.48/5 (13 votes)
5 Feb 2008CPOL4 min read 64.7K   302   41  
An article that discusses how to implement the observer pattern
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace ObserverTutorial
{
    public partial class Address_UC : System.Web.UI.UserControl, ObserverTutorial.App_Code.ISubject, ObserverTutorial.App_Code.IEntityDetails
    {
        //declare event  which will be fired when updates are made at the domain entity and declare a delegat to handle this event
        //public delegate void EntityChangHandeler(object sender);
        //public event EntityChangHandeler EntityChanged;


        private App_Code.AddressEntity Entity
        {
            get
            {
                if (this.ViewState["Entity"] == null)
                {
                    this.ViewState.Add("Entity", new App_Code.AddressEntity());
                }
                return this.ViewState["Entity"] as App_Code.AddressEntity;
            }
            set
            {
                this.ViewState["Entity"] = value;
            }
        }
        private List<App_Code.IObserver> m_ObserversList = new List<App_Code.IObserver>();

        private bool InsertMode
        {
            get
            {
                if (this.ViewState["Mode"] == null)
                {
                    this.ViewState.Add("Mode", true);
                }
                return (bool)this.ViewState["Mode"];
            }
            set
            {
                this.ViewState["Mode"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                this.ViewState.Add("Mode", "insert");

        }
        private bool SaveEntityData()
        {
            string mode = (this.ViewState["Mode"].ToString());
            if (Page.IsValid)
            {
                this.Entity.CountryName = txtCountry.Text;
                this.Entity.CityName = txtCity.Text;
                this.Entity.PostalCode = txtPostalCode.Text;
                this.Entity.State = txtState.Text;
                this.Entity.Street = txtStreet.Text;
                this.Entity.Buliding = txtBuilding.Text;
                this.Entity.EntityID = int.Parse(txtAddressId.Text);

                if (mode.Equals("insert"))
                {
                    this.Entity.CreatedBy = "Me!! the active uesr.";
                    this.Entity.CreationDate = DateTime.Now;
                    this.Entity.Status = "Saved";
                }

                else
                {
                    this.Entity.Status = "Updated";
                }

                this.Entity.LastModifiedBy = "Me!! the active uesr.";
                this.Entity.LastModificationDate = DateTime.Now;
                return true;
            }
            else
            {
                return false;
            }
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            bool result = SaveEntityData();

            /// After Subject object data is saved in a state notify observers
            if (result)
            {
                this.ViewState["Mode"] = "update";
                this.NotifyObservers();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
        #region IEntityDetails Members

        public DateTime CreationDate
        {
            get { return this.Entity.CreationDate; }
        }

        public DateTime LastModificationDate
        {
            get { return this.Entity.LastModificationDate; }
        }

        public string CreatedBy
        {
            get { return this.Entity.CreatedBy; }
        }

        public string LastModifiedBy
        {
            get { return this.Entity.LastModifiedBy; }
        }

        public string Status
        {
            get { return this.Entity.Status; }
        }

        public int EntityID
        {
            get { return this.Entity.EntityID; }
        }

        #endregion

        #region ISubject Members


        public List<ObserverTutorial.App_Code.IObserver> ObserversList
        {
            get { return this.m_ObserversList; }
        }

        public void AttachObserver(ObserverTutorial.App_Code.IObserver observer)
        {
            this.ObserversList.Add(observer);
        }

        public void DeAttachObserver(ObserverTutorial.App_Code.IObserver observer)
        {
            this.ObserversList.Remove(observer);
        }

        public void NotifyObservers()
        {
            foreach (App_Code.IObserver observer in this.ObserversList)
            {
                observer.Updateobject(this);
            }

            //Rais EnetityChanged event so observers can handle this event in their immplementation
            //if (this.EntityChanged != null)
            //{
            //    this.EntityChanged(this);
            //}
            
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer NTS
Palestinian Territory (Occupied) Palestinian Territory (Occupied)
Huthaifa lives happily in Jerusalem and enjoys his time developing web applications and learning new stuff.

Comments and Discussions