Appendix A. Appendix

Pluggable Adapter Pattern Example Code—CoolBook

See the following code for an example of the Pluggable Adapter Pattern:

using System; using System.Collections.Generic; using System.Windows.Forms; using System.Drawing; using System.Threading; // Adapter Pattern Example Judith Bishop Aug 2007 // Sets up a CoolBook // This is D-J's as changed for the book class AdapterPattern { // class SpaceBookSystem { public delegate void InputEventHandler(object sender, EventArgs e, string s); // Adapter public class MyCoolBook : MyOpenBook { static SortedList<string, MyCoolBook> community = new SortedList<string, MyCoolBook>(100); Interact visuals; public MyCoolBook(string name) : base(name) { // Create interact on the relevant thread, and start it! new Thread(delegate( ) { visuals = new Interact("CoolBook Beta"); visuals.InputEvent += new InputEventHandler(OnInput); visuals.FormClosed += new FormClosedEventHandler(OnFormClosed); Application.Run(visuals); }).Start( ); community[name] = this; while (visuals == null) { Application.DoEvents( ); Thread.Sleep(100); } Add("Welcome to CoolBook " + Name); } private void OnFormClosed(object sender, FormClosedEventArgs e) { community.Remove(Name); } private void OnInput(object sender, EventArgs e, string s) { Add("\r\n"); Add(s, "Poked you"); } public new void Poke(string who) { Add("\r\n"); if (community.ContainsKey(who)) community[who].Add(Name, "Poked you"); else Add("Friend " + who + " is not part of the community"); } public ...

Get C# 3.0 Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.