Design mode: Observer mode: http://www.cnblogs.com/li-peng/archive/2013/02/04/2892116.html
The Observer pattern is a pattern that we often use, such as notifying the client when doing a service with WCF to do something generally in designer mode.
Make a small example of a newspaper today to understand the viewer pattern Publisher + Subscriber = Observer Mode
In the picture below, People's Daily + subscriber = Observer Mode
As long as those who subscribe to the People's Daily, with a new newspaper will be sent to the subscribers,
When you do not want to order the time to cancel the subscription will not receive the newspaper.
Newspaper interface public interface Inewspaper {//Add subscriber void Registersubscriber (iSUBSCRiBE f_subscribe); Unsubscribe from void Removesubscriber (iSUBSCRiBE f_subscribe); Send newspaper void Sendpaper (); }//Subscriber public interface iSUBSCRiBE {//has a new newspaper will be executed notice void Hasnewpaper (); }//People's Daily public class Peoplenewspaper:inewspaper {private list<isubscribe> sublist = new List<isu Bscribe> (); public void Registersubscriber (iSUBSCRiBE f_subscribe) {sublist.add (f_subscribe); } public void Removesubscriber (iSUBSCRiBE f_subscribe) {if (Sublist.indexof (f_subscribe) >= 0) {Sublist.remove (f_subscribe); }}//Send newspaper ~ ~ public void Sendpaper () {foreach (iSUBSCRiBE _sub in sublist) { _sub. Hasnewpaper (); }}} public class Subhuman:isubscribe {//subscribers ' name PrivaTe string p_name; Public subhuman (String f_name) {p_name = F_name; }//Tell subscribers to have a new newspaper public void Hasnewpaper () {Console.WriteLine (P_name + "!! There's a new newspaper, please find it! "); }} started subscribing, and called the static void Main (string[] args) {Peoplenewspaper _paper = new Peoplenewspaper (); subhuman _xiaoming = new Subhuman ("Xiaoming"); subhuman _zhaoyun = new Subhuman ("Zhao Yun"); subhuman _liubei = new Subhuman ("Liu Bei"); Xiao Ming subscribe _paper. Registersubscriber (_xiaoming); Zhao Yun Subscribe _paper. Registersubscriber (_zhaoyun); Liu Bei subscribe _paper. Registersubscriber (_liubei); There's a new newspaper, _paper. Sendpaper (); Console.WriteLine ("---------------finished the newspaper------------------"); Xiao Ming didn't want to order, cancel the newspaper _paper. Removesubscriber (_xiaoming); There are new newspapers, there is no Xiao Ming's newspaper _paper. Sendpaper (); Console.ReadLine (); }
Design PATTERN: Observer mode