In Java's dozens of design patterns, a single example might be the easiest to understand. Because whether I am at present, or the occasional interview of others, can be a little more clearly, the basic is a single case model.
What is called a single case pattern. As the name suggests, is a single instance, the only instance. That is to say, for a Java class, his instance object can only be created at most one.
Well, a little Java based students know that the most basic way we create objects ourselves is to use the New keyword, created by the constructor of the class definition. For example, there is a class:
public class earth{Public Earth
() {
}
}
We create the object of the class ourselves, usually the new Earth (), so obviously, in this case we have a new object instance each time we go out.
This means that, as long as the class has a constructor that can be invoked externally, it is inevitable that the number of instances of the class cannot be controlled.
That is, if we want to have only one instance of this class, we must set the constructor access permission for the class to private so that it cannot be invoked externally . Just like the bottom:
public class earth{
private Earth () {
}
}
So the question is again, since the constructor for the class is private, how do we create an instance object of that class?
At this point, you need to know the basics: actually creating objects in Java can only be created using constructors, whether using frames (inside the framework) or using normal code directly.
In other words, even if the constructor of this class is private, we can only use the class constructor to create an instance object of this class.
In this way, it involves a Java very basic knowledge point, we know that the private modification of the method only the current class to use, then it is obvious, for the constructor is private class, we can only within the class of the class to create an instance object.
public class earth{The
Private Earth () {} The earth earth=new the earth
();
}
So here's the question again, it looks like we've created an instance object of that class in the current class, but we know that the Java non-static-decorated variable belongs to the object, and we can't create an instance object of the class externally, so we can't get the instance we created ourselves.
How to do it. This is the only class itself that can be accessed externally, so if external access to the instance that we created in the class, we can only make the instance a class, which means we need to make it static.
public class earth{The
Private Earth () {} The static Earth earth=new the Earth
();
}
OK, so, in fact, a basic single example is implemented, and we've made sure that the class will have only one instance object, just use Earth.earth to get it right.
However, this is not what we would like to say in the case of the canonical form of the pattern. Because in the Java specification, the properties of a class are generally hidden, meaning that the instance object created by itself needs to be declared private as well.
public class earth{The
Private Earth () {
} private static Earth earth=new the Earth
();
}
In this case, we do not seem to have access to the outside of the object of this class, how to do it. It can only provide an externally-exposed method for the external to obtain an object instance of the class based on this method. Of course, this method is also to belong to the class, so that in case there are no more objects to invoke:
public class earth{
private Earth () {
}
private static Earth earth=new ();
public static Earth getinstance () {return earth
;
}
Well, here we have a class of canonical singleton schemas that is truly complete, ensuring that the instance of the class is single and follows the basic Java specification.
In summary, you will find that there are three simple and necessary steps:
I. Privatization of constructors
Second, create your own private and static instance objects
provide an externally accessible static method that returns its own instance object
This pattern, referred to above, is called a A hungry man in a single case pattern.
What is a hungry man. Hungry, can be understood as impatient, that is, can't wait to eat things. In our code, which means that the instance object was created at the beginning, and the instance object was initialized immediately after the compilation.
So apart from the A hungry man type, there is a very common is the lazy type (there is actually a double lock mode, here for the time being), the basic code is as follows:
public class earth{
private Earth () {
}
private static Earth Earth=null;
public static Earth getinstance () {
if (earth==null) {
earth=new earth ();
}
return to Earth;
}
The difference with the top a hungry man is that it does not initialize the earth variable, but gives an initial value of NULL. It's just that it's written in a null, but it's actually not written.
Because even if we do not show it, the class is initialized with all variables after it has been compiled, and the base type has its own unique initial value, such as 0 for the type int, and the default initial value of the reference type is null.
At the same time, we put the instantiation of the object in the method provided to the external call, and make a certain judgment that the object is created only if it is null.
The judgment here is very necessary, and if you do not make judgments, then each call is a new object, cannot guarantee a single instance, with the direct constructor to create no difference .
So with the code, the lazy type is better understood. What is a slacker? Lazy, is to do everything to drag and drop, until you do not have to do. In our code, that is, when you have to get an instance object, you create an instance that is created the first time you call the GetInstance method.
The single case model is very simple and is widely used in practical projects. For example, some of the data is very small, directly defined dead, but sometimes because of changes in business requirements need to manually change some of the common data, we can not put in the database, but directly in the configuration file, and then loaded in the project, put in memory.