* design mode : Predecessors summed up the experience, is directly used by posterity.
* Singleton design mode : A class allows only one object to be used as a global access point for everyone to use.
Analysis
* 1. The user can only have one object
* 2. Global access point: The resulting object is the global access point. How to do the whole? Let the static be modified
* 3. How to provide out?
* 4. The function of?--single case for everyone to use
* The role of singleton classes:
* 1. Can realize the value between two objects
* 2. Many functions can be called at the global scope.
* Benefits: Can allow two of objects in a completely non-relational premise, the realization of the value of the transfer, reduce the coupling, improve the cohesion
* (Coupling, cohesion at the microscopic level: the relationship between classes and classes is described.) Benefits: We should try to improve cohesion, reduce coupling, can improve the robustness of the program, increase the portability of code, easy to implement module retroflex suffixation programming
* How to achieve low coupling high cohesion? When dealing with class and class relationships, the more tightly coupled the relationships between classes, the lower the cohesion. Conversely, the more loosely coupled the relationship, the higher the cohesion.
*/
Category: A hungry man type, lazy type: The format is similar, lazy type has a judgement, but the general framework of the two are like this, really do not understand can be back down will understand.
1. A hungry man -initialize at the same time as the variable is defined
Class singleinstance{//creates a singleton class
//2. Creates a property of the current type within the class and assigns a value--an object of the current class is obtained inside the class
//The variable is privatized, making it inaccessible directly to the outside world Give the variable a static modifier to make it a global access point
private static singleinstance singleinstance = new SingleInstance ();
//1. Privatize the construction method
Private singleinstance () {
}
//3. Use a public method to move the variable out and set the method to static
public static Singleins Tance getinstance () {
return singleinstance;
}
}
2, lazy-type -start by just defining variables, when to use when to assign
class singleinstance1{
private static SingleInstance1 singleinstance = null ;// do not assign first
Private SingleInstance1 () {
}< br> public static SingleInstance1 getinstance () {
if (singleinstance = = null) { //determine if null
singleinstance = new SingleInstance1 ();//Use to assign value
}
return singleinstance;
}
int num;//This num is used for an example after
}
Main function:
public class Demo5 {
Public static void Main (string[] args){
SingleInstance1 S1 = singleinstance1.getinstance ();
SingleInstance1 s2 = singleinstance1.getinstance ();
System.out.println (S1 = = s2);//Output True after run, indicating that the same object was obtained
// Example :
Function: Pass the value of NUM1 in Class A to the num2 of Class B object
A = new A ();
A. NUM1 = 4;
b b = new B ();
b.num2 = A.NUM1; Method One: Direct assignment, the member variable of the general class is private, so it is not recommended
B.test (a); Method Two: Indirect assignment by means of the transfer parameter
Method Three: Through a single example to achieve the value:
A.ceshi1 ();
B.ceshi2 ();
}
}
Test the function of a single case
Class a{
int num1;
Test passed a single-case pass value
public void Ceshi1 () {
SingleInstance1 S1 = singleinstance1.getinstance ();
s1.num = NUM1;//pass NUM1 to Num
}
}
Class b{
int num2;
Assigning values by passing parameters
public void Test (a a) {
num2 = A.NUM1;
}
Test passed a single-case pass value
public void Ceshi2 () {
SingleInstance1 s2 = singleinstance1.getinstance ();
num2 = s2.num;//Pass num to num2
}
}
Article original, reproduced please indicate the source thank you
Summary and examples of Java single-instance design patterns