C # design mode,
Billing Mode
Make sure that a class has only one instance and provides a global access point to it.
The following code is used:
/// <Summary> /// single-profit mode class ///// </summary> public class interest {/// <summary> /// defines a single-profit lock/ // </summary> private static object _ lock = new object (); /// <summary> /// declare a private global access variable // </summary> private static interest ins = null; /// <summary> /// private constructor definition cannot be instantiated using new // </summary> private interest () {}/// <summary> /// create a static method to return the instance of this class /// </summary> /// <returns> </returns> public static interest GetIntance () {// when the first thread runs here, the locker object will be locked. // when the second thread runs this method, first, it is detected that the locker object is in the "lock" status, and the thread will suspend waiting for the first thread to unlock lock (_ lock) {if (ins = null) {ins = new interest () ;}}/* Better implementation method using double locks ** // if (ins = null) {// lock (_ lock) // {// if (ins = null) {// ins = new interest (); //} return ins ;} public string ShowMessage (string message) {return "hellow" + message ;}}View Code
Abstract Factory Model
Before that, let's take a look at the concept of an abstract class;
What is an abstract class? Methods must be implemented and not instantiated to inherit from sub-classes. abstract is used for modification;
Define an abstract class:
1 public abstract class abstractFactory 2 {3 4 /// <summary> 5 // define the eating base class method 6 /// </summary> 7 /// <returns> </returns> 8 public abstract eat kindeat (); 9 /// <summary> 10 /// defines the singing base class method 11 /// </summary> 12 /// <returns> </returns> 13 public abstract sing kindsing (); 14 15}View Code
Define a subclass, inherit the abstract class, and implement its method:
public class person : abstractFactory { public override eat kindeat() { return new personeat(); } public override sing kindsing() { return new personsing(); } }View Code
Abstract Factory my own understanding is to define some common features in the factory class, such as eating, singing, and so on. At the same time, we need to analyze specific things. for example, if a person eats rice, the bird does not eat rice or worms. while humans and birds share the public characteristics of eating, but they only eat different things.
So here we can use abstraction to implement the following:
/// <Summary> /// abstract Factory method base class /// </summary> public abstract class abstractFactory {/// <summary> /// defines the meal base class Method /// </summary> /// <returns> </returns> public abstract eat kindeat (); /// <summary> /// defines the singing base class method /// </summary> /// <returns> </returns> public abstract sing kindsing ();} /// <summary> /// human // </summary> public class person: abstractFactory {public override eat kindeat () {return new personeat ();} public override sing kindsing () {return new personsing () ;}/// <summary> // birds /// </summary> public class brid: abstractFactory {public override eat kindeat () {return new brideat ();} public override sing kindsing () {return new bridsing ();}} /// <summary> /// class for meals /// </summary> public abstract class eat {public abstract void print ();} /// <summary> /// singing class /// </summary> public abstract class sing {public abstract void print ();} /// <summary> /// human eating class /// </summary> public class personeat: eat {public override void print () {Console. writeLine ("My tm hot pot") ;}} public class brideat: eat {public override void print () {Console. writeLine ("bad bugs") ;}} public class personsing: sing {public override void print () {Console. writeLine ("");} public class bridsing: sing {public override void print () {Console. writeLine (" ");}}View Code
Call method:
// Define a Task t1 = new Task () => {// call the ticket Console. writeLine (interest. getIntance (). showMessage ("red"); Console. writeLine (interest. getIntance (). showMessage ("") ;}; // enable Task t1.Start (); // wait until the thread finishes executing the Task. waitAll (t1); // Abstract Factory mode abstractFactory person = new person (); eat e = person. kindeat (); e. print ();View Code
The above is my simple understanding!