single-state design patterns in Java
2011-09-23 16:38:46| Category: Java | Tags:technology! | Report | Font size Subscription
This blog post is a blog post from a blogger called "Junny's Blog". Feel this blog is really good, so take it out here to share! (I just made a few changes to the code.)
A concept:
Design pattern: The design pattern refers to the optimized code structure, programming style, and problem-solving ideas after a lot of practice summarization and theory.
Single-state design mode: Take certain methods to ensure that in the whole software system, only one object instance can be generated for a class, and that class only provides an instance method to get its object.
Two implementations:
To implement a single mode in Java, you only need to perform the following three steps:
1. Declare the constructor as private. This allows the object to be generated only inside the class, not externally through new.
2. Generate a static instance within the class.
3. Provide a static method for externally obtaining instances of this class.
Three examples:
Class Chinese {
Static Chinese Chinese = new Chinese ();
Private Chinese () {
}
public static Chinese getinstance () {
return Chinese;
}
}
Improved:
Class Chinese {
Static Chinese Chinese = null;
Private Chinese () {
}
public static Chinese getinstance () {
if (Chinese = = null) {
Chinese = new Chinese ();
}
return Chinese;
}
}
This is the information found on the Internet;
Here is the program code you wrote:
Package mypkg;
Class Single {
Static single ref = NULL; = new single ();//Because the static function getinstance () is guaranteed to invoke the variable ref, it must be set to static.
Private single () {//set to private to prevent external calls from generating other objects
System.out.println ("Hao");
}
public static single getinstance () {//In order for the outside function to produce a single object, only through this function can production, so it must be public and static
if (ref = = NULL) {
ref = new single ();
}
return ref;
}
}
Class Other {
public static int i = 0;
Public Other () {
System.out.println ("Form i=" + (++i));
}
public static Other Getother () {
Return (new Other ());
}
}
public class TestSingle {
public static int i = 0;
public static void Main (string[] args) {
Single obj1 = Single.getinstance ();
Single obj2 = Single.getinstance ();
Single obj3 = Single.getinstance ();
Other ob1 = Other.getother ();
Other OB2 = Other.getother ();
System.out.println (obj1 = = obj2) && (obj2 = = obj3) && (obj1 = obj3));
System.out.println ("******\n" + (Ob1 = = Ob2));
}
}
The result of the execution is:
Hao
Form I=1
Form i=2
True
******
False
It can be seen that only one object is produced in the single-state design mode, OBJ1, Obj2, obj3 are the same object, so only one Hao is output in the result. Rather than a single-state mode, many objects can be produced, such as Ob1 and OB2, which are 2 different objects.
http://liuxiaowei199001.blog.163.com/blog/static/193805325201182343020758/
Single-state design Patterns in Java