Thursday, July 25, 2013

Observer Design Pattern Example

Announcement Ticker
 public class Announcement
    {
        public string Company { get; set; }
        public string Content { get; set; }
        
       
    }
public class AnnouncementChangeEventArgs
    {
        private Announcement announcement;
        public AnnouncementChangeEventArgs(Announcement announcement)
        {
            this.announcement = announcement;
        }
        public Announcement Announcement
        {
            get
            {
                return this.announcement;
            }
            
        }
    }
public class AnnouncementTicker
    {
        private Announcement announcement;
        public Announcement Announcement
        {
            get
            {
                return announcement;
            }
            set
            {
                this.announcement = value;
                OnAnnouncementChange(new AnnouncementChangeEventArgs(this.announcement));
            }
        }
        public event EventHandler AnnouncementChange;

        protected virtual void OnAnnouncementChange(AnnouncementChangeEventArgs e)
        {
            if (AnnouncementChange != null)
            {
                AnnouncementChange(this, e);
            }
        }
    }
Microsoft Announcement Observer
public class MicrosoftObserver
    {
         
        public MicrosoftObserver(AnnouncementTicker ticker)
        {
            ticker.AnnouncementChange += new EventHandler(bt_BlogChange);
        }

        private void bt_BlogChange(object sender, AnnouncementChangeEventArgs e)
        {
            UpdateDashboard(e.Announcement);
        }

        private void UpdateDashboard(Announcement announcement)
        {
            if (announcement.Company == "0")
            {
                var isNewWindow = true;                 
                MicrosoftDashboard d = ((MicrosoftDashboard)Application.OpenForms["MicrosoftDashboard"]);
                if (d != null && d.Visible == true)
                {
                    isNewWindow = false;
                }               

                d = d ?? new MicrosoftDashboard();
                d.Text = "Microsoft Dashboard";
                var existingContent = d.BlogContent.Text;
                d.BlogContent.Text = string.Format(existingContent + "\n" + announcement.Content);
                if (isNewWindow)
                    d.Show();
            }
        }
        
    }
Google Announcement Observer
public class GoogleObserver
    {
        public GoogleObserver(AnnouncementTicker ticker)
        {
            ticker.AnnouncementChange += new EventHandler(bt_AnnouncementChange);
        }

        private void bt_AnnouncementChange(object sender, AnnouncementChangeEventArgs e)
        {
            UpdateDashboard(e.Announcement);
        }

        private void UpdateDashboard(Announcement announcement)
        {
            if (announcement.Company == "1")
            {
                var isNewWindow = true;
                GoogleDashboard d = ((GoogleDashboard)Application.OpenForms["GoogleDashboard"]);
                if (d != null && d.Visible == true)
                {
                    isNewWindow = false;
                }

                d = d ?? new GoogleDashboard();
                d.Text = "Google Dashboard";
                var existingContent = d.BlogContent.Text;
                d.BlogContent.Text = string.Format(existingContent + "\n" + announcement.Content);
                if (isNewWindow)
                    d.Show();
            }
            
        }

        
    }

Ticker Windows Form
public partial class TIcker : Form
    {
        public TIcker()
        {
            InitializeComponent();
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            AnnouncementTicker ticker = new AnnouncementTicker();

            MicrosoftObserver msft = new MicrosoftObserver(ticker);
            GoogleObserver ggl = new GoogleObserver(ticker);

            ticker.Announcement = new Announcement { Company = Company.SelectedIndex.ToString(), Content = Content.Text };
               
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            Company.Items.Add(new { Name ="Microsoft", Id = 1});
            Company.Items.Add(new { Name = "Google", Id = 2 });
            Company.DisplayMember = "Name";
            Company.ValueMember = "Id";
            Company.SelectedIndex = 0;
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

No comments:

Post a Comment