/// <summary> /// Summary description for Observer. /// Observer Pattern 观察者 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Observer { void sendNotify(string message); }
/// <summary> /// Summary description for Subject. /// Observer Pattern 观察者 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public interface Subject { void registerInterest(Observer obs); }
/// <summary> /// Observer Pattern 观察者 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ListObsForm : Form, Observer { /// <summary> /// /// </summary> public ListObsForm(Subject subj) { InitializeComponent(); init(subj); } /// <summary> /// /// </summary> /// <param name="subj"></param> public void init(Subject subj) { subj.registerInterest(this); } /// <summary> /// /// </summary> /// <param name="message"></param> public void sendNotify(string message) { lsColors.Items.Add(message); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ListObsForm_Load(object sender, EventArgs e) { } }
/// <summary> /// Observer Pattern 观察者 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ColObserverForm : Form, Observer { private Brush bBrush; // private System.Windows.Forms.PictureBox pic; private Font fnt; private Hashtable colors; private string colName; /// <summary> /// /// </summary> /// <param name="subj"></param> private void init(Subject subj) { subj.registerInterest(this); fnt = new Font("arial", 18, FontStyle.Bold); bBrush = new SolidBrush(Color.Black); pic.Paint += new PaintEventHandler(paintHandler); colors = new Hashtable(); colors.Add("red", Color.Red); colors.Add("blue", Color.Blue); colors.Add("green", Color.Green); colName = ""; } /// <summary> /// /// </summary> /// <param name="message"></param> public void sendNotify(string message) { colName = message; message = message.ToLower(); Color col = (Color)colors[message]; pic.BackColor = col; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void paintHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.DrawString(colName, fnt, bBrush, 20, 40); } /// <summary> /// /// </summary> public ColObserverForm(Subject subj) { InitializeComponent(); init(subj); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ColObserverForm_Load(object sender, EventArgs e) { } }
调用:
/// <summary> /// Observer Pattern 观察者 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class ObserverPatternForm : Form, Subject { private ArrayList observers; /// <summary> /// /// </summary> private void init() { EventHandler evh = new EventHandler(opButton_Click); opRed.Click += evh; opBlue.Click += evh; opGreen.Click += evh; observers = new ArrayList(); ListObsForm lobs = new ListObsForm(this); lobs.Show(); ColObserverForm colObs = new ColObserverForm(this); colObs.Show(); } /// <summary> /// /// </summary> /// <param name="obs"></param> public void registerInterest(Observer obs) { observers.Add(obs); } /// <summary> /// /// </summary> public ObserverPatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ObserverPatternForm_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void opButton_Click(object sender, System.EventArgs e) { RadioButton but = (RadioButton)sender; for (int i = 0; i < observers.Count; i++) { Observer obs = (Observer)observers[i]; obs.sendNotify(but.Text); } } }
输出:
标签:Observer,Pattern,void,private,CSharp,new,message,public From: https://www.cnblogs.com/geovindu/p/16746309.html