Design Patterns ---- design rules and (simple factory, factory method, Singleton mode)

Source: Internet
Author: User
I. Design Principles

Design ModeIt is a successful or effective solution for people to solve repeated problems in a specific environment.

Current types of design patterns:Gof's 23 + "simple factory model" = 24.

The role of the design pattern:High Cohesion and low coupling
Principles of software development:

Dimit: Multi-combination and less inheritance
Regular replacement: Polymorphism
Merging and reuse: multiple combinations with less inheritance
Dependency reversal:
Single:
Open/closed:

 

Ii. Simple Factory

An abstract parent class, N Implementation classes, and a factory

Different parameters and results different parent classes return a simple switch Factory (Deformation

Code example:

1 using system; 2 namespace day24 3 {4 public abstract class phone // parent class 5 {6 Public String phonename; 7 public abstract void show (); 8} 9 public class PSK // factory 10 {11 public static phone createphonebyname (string phonename) 12 {13 phone returnvalue; 14 switch (phonename) 15 {16 case "apple ": 17 returnvalue = new applexrs (); 18 returnvalue. phonename = "applexrs"; 19 Return returnvalue; 20 case "mi": 21 retur Nvalue = new mimix3 (); 22 returnvalue. phonename = "mimix3"; 23 return returnvalue; 24 case "Huawei": 25 returnvalue = new receivweics (); 26 returnvalue. phonename = "invalid weics"; 27 return returnvalue; 28 case "meitu": 29 returnvalue = new meitu (); 30 returnvalue. phonename = "meitu"; 31 return returnvalue; 32} 33 return NULL; 34} 35} 36 public class using weics: Phone // subclass 0137 {38 public override void show () 39 {40 con Sole. writeline ("I'm a {0} mobile phone. I'm presenting the latest features! ", Phonename); 41} 42} 43 public class mimix3: Phone // subclass 0244 {45 public override void show () 46 {47 console. writeline ("I'm a {0} mobile phone. I'm presenting the latest features! ", Phonename); 48} 49} 50 public class meitu: Phone // subclass 0351 {52 public override void show () 53 {54 console. writeline ("I'm a {0} mobile phone. I'm presenting the latest features! ", Phonename); 55} 56} 57 public class applexrs: Phone // subclass 0458 {59 Public override void show () 60 {61 console. writeline ("I'm a {0} mobile phone. I'm presenting the latest features! ", Phonename); 62} 63} 64 internal class program // implement 65 {66 public static void main (string [] ARGs) 67 {68 // pass the parameter to Foxconn, foxconn accepts the parameter and returns 69 phone createphone = PSK. createphonebyname ("meitu"); 70 createphone. show (); 71} 72} 73}
View code

Advantages and disadvantages:

Advantage: separated object creation and use.

You do not need to remember the specific class name. Remember the parameters to reduce the user's memory.

Disadvantages: Too heavy responsibility for the factory. Once unable to work, the system will be affected.

Increase the number of classes in the system, and increase the complexity and understanding.

In violation of the "open and closed principle", the factory logic needs to be modified to add new products, and the factory becomes more and more complex.

Iii. factory methods

An item corresponds to a factory! Self-sufficiency

Code example:

1 using system; 2 namespace factory method 3 {4 public abstract class absphone // mobile phone parent class 5 {6 public abstract void show (); 7} 8 public abstract class absfac // factory parent class 9 {10 public abstract absphone createphone (); 11} 12 public class Huawei: absphone // mobile phone subclass 0113 {14 public override void show () 15 {16 console. writeline ("my mobile phone"); 17} 18} 19 public class program weifac: absfac // factory subclass 0120 {21 public override absphone createphone () 22 {23 return New Huawei (); 24} 25} 26 internal class program // main function 27 {28 // an object corresponds to a factory 29 public static void main (string [] ARGs) 30 {31 console. read (); 32} 33} 34}
View code

Advantages and disadvantages:

Advantage: you do not need to remember the specific class name, or even remember the specific parameters.

Separated object creation and use.

The scalability of the system is also very good, without modifying the interface and the original Class

Disadvantage: Increase the number of classes in the system, and increase complexity and understanding.

This increases the abstraction and difficulty of the system.

Iv. Singleton Mode

Single ----> unique

Example ----> Instance Object

Steps:

1). Define static private objects

2). Privatization of Constructors

3) define a static return value for this type of method. Generally, getinstance/getinit is the method name.

The Singleton mode includes lazy and lazy, and the lazy thread is not safe. It is best to use ELE. Me.

Code instance

Lazy mode: Post-instantiation

1 using system; 2 namespace Singleton Mode 3 {4 // defines static private object 5 // constructor privatization 6 // defines a static return value for this type of method, generally using getinstance/getinit as the method name 7 class Singleton 8 {9 Private Static Singleton _ Singleton; // step 10 private Singleton () // Step 11 {12 // initialize 13} 14 public static Singleton getinstance () // Step 15 {16 if (null = _ Singleton) 17 {18 _ Singleton = new Singleton (); 19 console. writeline ("Haha"); 20} 21 return _ Singleton; 22} 23} 24 internal class program25 {26 public static void main (string [] ARGs) 27 {28 for (INT I = 0; I <100; I ++) 29 {30 Singleton. getinstance (); 31} 32} 33} 34}
View code

Hunger mode: First instantiate

1 public class hungrysingle 2 {3 4 Private Static hungrysingle sinstance = new hungrysingle (); // Step 5 6 private hungrysingle () // Step 2 7 {8 9} 10 11 public static hungrysingle getinstance () // step 3 12 {13 return sinstance; 14} 15 16}
View code

 

Statement of the single-Profit Mode in unity:

 1 public sealed class SingletonMoBehaviour: MonoBehaviour 2 {  3     private static volatile SingletonBehaviour instance;  4     private static object syncRoot = new Object();  5     public static SingletonBehaviour Instance  6     {  7         get   8         {  9             if (instance == null)  10             { 11                 lock (syncRoot)  12                 { 13                     if (instance == null)  {14                         SingletonBehaviour[] instances = FindObjectsOfType<SingletonBehaviour>();15                         if (instances != null){16                             for (var i = 0; i < instances.Length; i++) {17                                 Destroy(instances[i].gameObject);18                             }19                         }20                         GameObject go = new GameObject("_SingletonBehaviour");21                         instance = go.AddComponent<SingletonBehaviour>();22                         DontDestroyOnLoad(go); 23                     }24                 } 25             } 26             return instance; 27         } 28     } 29 }
View code

Lazy and lazy are different, but they can be used in unity, because multithreading is not involved !!!

Extended knowledge:

Thread: parallel for direct CPU Switching
Process: the unit of resource allocation.

Design Patterns ---- design rules and (simple factory, factory method, Singleton mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.