Observer-intermediary design mode (non-delegate implementation mode) and Observer Design Mode

Source: Internet
Author: User

Observer-intermediary design mode (non-delegate implementation mode) and Observer Design Mode

Implemented through interfaces to achieve loose coupling.

Main form

public partial class mainform : Form    {        public mainform()        {            InitializeComponent();        }        private void butMessage_Click(object sender, EventArgs e)        {            childform1 c1 = new childform1();            c1.Show();            childform2 c2 = new childform2();            c1.messageonList.Add(c2);            c2.Show();            childform3 c3 = new childform3();            c1.messageonList.Add(c3);            c3.Show();        }    }

Subform 1

Public partial class childform1: Form {public childform1 () {InitializeComponent (); messageonList = new List <ImessageOn> ();} // put this set in our observer public List <ImessageOn> messageonList {get; set;} private void butMessage_Click (object sender, EventArgs e) {foreach (var messageon in messageonList) {messageon. receiveMessage ("James ");}}}

Subforms 2 and 3

public partial class childform2 : Form,ImessageOn    {        public childform2()        {            InitializeComponent();        }        public void ReceiveMessage(string str)        {            this.textMessage.Text = str + " " + DateTime.Now.ToString();        }    }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.