The problem with the meta-mode is that objects are created repeatedly! This you will think, I use the singleton mode can be solved. Yes.
Both the enjoy meta mode and the singleton mode are to maintain the uniqueness of the object:
What is the difference between the enjoy meta mode and the singleton mode?
The enjoy meta mode is the external responsibility of the object (usually factory) to maintain the uniqueness of the object.
Singleton mode is the object's own responsibility to maintain the uniqueness of the object.
Crossword Puzzles:
Example 1:
Static voidMain (string[] args) {Iword S1=NewS (); Iword e=NewE (); Iword S2=NewS (); Iword S3=NewS (); Iword I=NewI (); Iword o=NewO (); Iword N=NewN (); Console.WriteLine ("Scrabble game: {0}{1}{2}{3}{4}{5}{6}", S1. Getword (), E.getword (), S2. Getword (), S3. Getword (), I.getword (), O.getword (), N.getword ()); Console.read (); }
Public Abstract class Iword { publicabstractstring Getword (); }
Public class S:iword { public s () { Console.WriteLine ("S is created " ); } Public Override string Getword () { return"s"; } }
Example 2:
Static voidMain (string[] args) {Iword s=NewS (); Iword e=NewE (); Iword I=NewI (); Iword o=NewO (); Iword N=NewN (); Console.WriteLine ("Scrabble game: {0}{1}{2}{3}{4}{5}{6}", S.getword (), E.getword (), S.getword (), S.getword (), I.getword (), O.getword (), N.getword ()); Console.read (); }
s in Example 1 instance 3 times, Example 2 has only one instance. The effect is the same. Example 2 ratio 1 more memory and space savings, and maintainability
Example 3:
static void Main (string [] args) {Iword s = Wo Rdfactory.getword (WORDENUM.S); Iword e = Wordfactory.getword (WORDENUM.E); Iword i = Wordfactory.getword (WORDENUM.I); Iword o = Wordfactory.getword (WORDENUM.O); Iword n = Wordfactory.getword (WORDENUM.N); Console.WriteLine ( Scrabble: {0}{1}{2}{3} {4} {5} {6} "
Public classWordfactory {/// <summary> ///defines a static dictionary that is maintained to ensure that an object instance has only one/// </summary> Staticdictionary<string, iword> Dictionaryword =Newdictionary<string, iword>(); /// <summary> ///This lock is for multi-threading and determines that only one thread can access/// </summary> Static ObjectObjectlock =New Object(); Public StaticIword Getword (wordenum wordenum) {Switch(wordenum) { CaseWORDENUM.S:///if the object already exists, then the thread will not wait for the lock to return directly, thus saving the cost of the thread lock . if(!Dictionaryword.containskey (wordenum. ToString ())) {///lock to ensure that only one thread can access Lock(objectlock) {///Check for Presence if(!Dictionaryword.containskey (wordenum. ToString ())) {///If it does not exist, createDictionaryword.add (Wordenum. ToString (),NewS ()); } } } ///return Object returnDictionaryword[wordenum. ToString ()]; CaseWORDENUM.E:return NewE (); CaseWORDENUM.I:return NewI (); CaseWORDENUM.O:return NewO (); CaseWORDENUM.N:return NewN (); default: Throw NewException ("Error"); } } } Public enumWordenum {s, E, I, O, n}
The S object was used 3 times and was created only 1 times
Example 3 uses a factory to manage the objects to be created. Simultaneous support for multi-threaded mode of enjoyment
Enjoy meta mode