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(); } }