Design Mode-singleton mode and observer Mode
Observer mode example: in the game scenario, a little boy is thrown near many devils and exploded. Based on the power of the bomb, the blood volume of the devils after the explosion is calculated. Assuming that some devils have defensive equipment, some devils do not have any defense tools. Analysis: In this case, the observer mode is ideal, because the observer mode processes one-to-many dependencies between objects. When an object changes, all other objects dependent on them must be notified and updated. Definition: In the observer mode, the boys mentioned above are called themes, while little devils are called observers. I will use the code below to demonstrate the example. Define the topic in the Observer mode. Copy the code // bomb public class bomb {// bomb Name public string Name; // bomb attack distance from public int Length; // bomb attack force public int ATK; // During simple periods, I only have one bomb here. Public bomb () {Name = ""; Length = 100; ATK = 1000;} public DeBeng debeng; // explosion public void beng () {if (debeng! = Null) {debeng (this) ;}}// define the delegate to add public delegate void DeBeng (bomb) to passers-by affected by the explosion ); copy the Code to define the observer in the Observer mode. Copy the code // The Observer, and the passer-by public class roadPeople {// The distance from the bomb to the public int Length; // the passer-by Name public string Name; // blood volume public int LifeLength; // shield resistance public int ShieldLth; /// <summary> /// initialize passers-by /// </summary> /// <param name = "name"> name </param> /// <param name = "lgth"> bomb distance </param> // <param name = "llth"> life value </param> // /<Param name = "something"> resistance </param> public roadPeople (string name, int lgth, int llth, int…) {Length = lgth; Name = name; lifeLength = llth; ShieldLth = something;} // I wrote the same solution here. In fact, there are different processing methods based on different cabinets, in this way, we can see the powerful observer mode. For the sake of simplicity, we have not defined so many types of public void Beated (bomb bom) {// shield against int th = this. shieldLth-bom.ATK; // string info = ""; if (th> 0) {// shield niuqiang, do not drop the blood info = "I am: "+ this. name + ". My shield is even better than blood loss ";} else {// blood loss int h = th + this. lifeLength; // determines if (h> 0) {// info = "I am:" + this. name + ". Drop blood: "+ (-th) +. ";} Else {// dead info =" I am: "+ this. Name + ". In the last sentence, I hope teacher Cang will go with me! ";}} Console. WriteLine (" ah, oh, amount, amount, I rely on, awesome. "+ Info) ;}} use the topic and observer to copy the code static void Main () {// List of passers-by sets <roadPeople> list = new List <roadPeople> () {// passerby A, 10 meters away from the bomb, with a blood volume of 100, with Level 1 shield new roadPeople ("passerby a", 40,100, 1), // passerby B, 20 meters away from the bomb, 1000 of blood volume, with 10 levels of shield new roadPeople ("passers-by B", 40,100,400 0), new roadPeople ("passers-by C", 50), new roadPeople ("passers-by Ding ", 1000,30, 1)}; // instantiate the bomb bom = new bomb (); // Add the attacker foreach (roadPeople rp in list) {if (bom. length-rp. length)> 0) {bom. debeng + = new DeBeng (rp. beated) ;}}// bomb explosion bom. beng (); Console. readLine ();} briefly describes the process of constructing the observer mode: the subject can be changed as a parameter, and the observer has its own attributes and the same signature response method, add the method of the observer object to the event, and then pass the topic object as a parameter to the event. In this way, based on the attributes of the observer, and the calculation method to obtain the notification update. Observer mode effect: the observer promotes the abstract coupling of the subject. The subject does not know the details of the observer. The observer can change the notification of the subject based on its attribute functions. However, the observer also has its own disadvantage: when a series of theme changes occur, the observer must update the theme in batches. If the update cost is high, the solution is to notify the observer according to the type requirement, instead of blindly notifying all observers. Singleton mode (Singleton mode): you only have one dad, and your mom only has one husband. However, he is the same person. You have eaten in the evening, you asked your dad to tell you about your childhood. Your mom asked your dad to talk about what they did when they fell in love. Dad said that the other two of you did not have the same treatment. Analysis: in this case, Dad has only one instance and cannot be created multiple times. Therefore, it is suitable for the singleton mode. Singleton mode: first, make sure that Dad cannot be created multiple times, because Dad has only one, making us unique. Singleton in singleton mode ensures that the class has only one copy code // the Dad in the singleton mode has only one public class Dad {// Dad static dad Dad = null; static readonly object padlock = new object (); Dad () {// full, rest for 2 seconds first Thread. sleep (2000);} // The story content is private string content = ""; // The public void tell () {lock (padlock) {if (Thread. currentThread. name = "mama") {content = "my wife's screen is so beautiful that I can't think of it! ";} Else if (Thread. CurrentThread. Name =" me ") {content =" Son, we love you very much when you were a child. ";}Else {content =" except for my wife and children, I'm not expected to be told by others! ";}}/// Get the story content. public string GetCounter () {return content ;}// there is only one dad. It cannot be created multiple times. Let's call it dad, dad will see public static dad sayDad {get {if (dad = null) {lock (padlock) {if (Dad = null) {dad = new Dad () ;}} return dad ;}}} copy the code using a single-piece object to copy the code /// <summary> /// thread work /// </summary> public static void DoSomeWork () {// construct the display string results = ""; // call Dad. The only dad is Dad = Dad. sayDad; // start telling the story dad. tel L (); results + = ""; results + = Thread. currentThread. name. toString () + "-->"; results + = "I want to say to you:"; results + = dad. getCounter (). toString (); results + = "\ n"; Console. writeLine (results); // clear the display string results = "";} // in Dad's world, there will always be two threads with me: public void StartMain () {Thread thread0 = Thread. currentThread; thread0.Name = "me"; Thread thread1 = new Thread (new ThreadStart (DoSomeWork); thread1.Name = "mama "; Thread thread2 = new Thread (new ThreadStart (DoSomeWork); thread2.Name = "badegg"; Thread. sleep (1000); thread1.Start (); Thread. sleep (1000); thread2.Start (); Thread. sleep (1000); // Thread 0 only executes the same work as other threads DoSomeWork ();} public static void Main (string [] args) {CountMutilThread cmt = new CountMutilThread (); cmt. startMain (); Console. readLine ();} Singleton mode: ensures that a single piece has only one instance (not inheriting, not structuring, or other means), and provides an access point for the instance. Another good way for a singleton is to public class Dad {// store the unique instance static Dad = new dad (); // do not let the instantiation of private Dad () {}// there is only one Dad. It cannot be created multiple times. If you call dad, Dad will see public static dad sayDad () {return Dad ;} object ss = new object (); // The story content is private string content = ""; // The public void tell () {lock (ss) {if (Thread. currentThread. name = "mama") {content = "my wife's screen is so beautiful that I can't think of it! ";} Else if (Thread. CurrentThread. Name =" me ") {content =" Son, we love you very much when you were a child. ";}Else {content =" except for my wife and children, I'm not expected to be told by others! ";}}/// Get the story content public string GetCounter () {return content ;}}