1 //Singleton mode: A hungry man type and lazy type2 //single-case mode3 4 /*5 design Pattern: The solution to solve a certain kind of problem (thought). 6 Singleton (Singleton): Design mode:7 The Learning design pattern must first clarify what problem it solves. 8 9 What is the problem with singleton mode? Ten you can guarantee the object uniqueness of a class. One A scenarios: For example, multiple programs use data from a configuration file, and you want to implement - data sharing and exchange. You must encapsulate multiple data into an object. and - multiple programs operate on the same object. Which means that this configuration must be guaranteed the the uniqueness of the file. - - How can we guarantee the uniqueness of the object? - 1, a class can produce multiple objects as long as the constructor is provided. + There is absolutely no guarantee of uniqueness. - since the number is not controllable, simply, do not allow other programs to establish objects. + A 2, do not let other programs create objects, what is the image? at simply, you create an object in this class, so the benefits are what, controllable!. - - 3, after the creation is completed, is not to give other programs to provide access to the way. - - How do you implement this step? - 1, how can you not let other programs create objects? in direct privatisation constructor. objects created by other programs are not initialized so that other programs - You cannot create objects, but you can create objects of this class in despicable. to 2, a new object of this class is directly in this class. + 3, define a function that other programs can get to the object of this class through this function. - */ the //code manifests. * $ //"The object has been generated when the method is called"Panax Notoginseng //a hungry man type. - class Single the { + //privatisation constructor. A PrivateSingle () {} the + //2, create an object of this class. - Private Static /*Final*/Single S =NewSingle ();//because the GetInstance method is $ //static, so this method can only $ //access to the static, so here must - //add static keyword modifiers. - //because this object s is static, it can be used by other the //The program directly has the class name called, but it is not safe, - //object is not controllable, so in order not to let other programs directlyWuyi //called by the name of the class, adding the private the //key word. - Wu //3, define a method to return this object. - Public StaticSingle getinstance (intx) About { $ //because this method is to return an object, - //is to let other programs get the object's - //so the permissions for this method are large enough, so - //for public. A if(x<0) + returns; the Else - return NULL; $ } the } the the //the lazy loading method for a single case. (Singleton: Individual object) "When you get the method, you produce the object." the //lazy type. - classSingle2 in { the Private StaticSingle2 s2 =NULL; the About PrivateSingle2 () {}//privatisation constructor. the the Public StaticSingle2 getinstance () the { + if(s2==NULL) -S2 =NewSingle2 (); the returnS2;Bayi } the } the - - classSingledemo the { the Public Static voidMain (string[] args) the { the //to get the single object, call the Geiinstance method, - //since it is not possible to call through the object, only with the class name, then the //This method must be static. the theSingle SS = Single.getinstance (2);94Single SS2 = Single.getinstance (3); theSYSTEM.OUT.PRINTLN (ss = = SS2);//returns True to know that SS and Ss2 point to the same object. the theSingle2 s =single2.getinstance ();98Single2 S1 =single2.getinstance (); AboutSystem.out.println (s = = S1);//returns true to know that s and S1 point to the same object. - }101 }102 103 /*104 In a hungry man, you can add the final keyword when you create this type of object in this class, but the lazy type can not, try to analyze the reason. 106 */
"Singleton pattern in Java--two kinds"