Core--privatize the constructor inside the class, generating the object internally, and returning the reference to the instantiated object through the class. Static method (Statics)
Design patterns are optimized code results, programming styles, and problem-solving thinking methods after a great deal of practice summarization and theory. Design patterns are like classic games, different games we use different games.
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.
To implement a single mode in Java, you only need to perform the following three steps:
1. Declare the access permission for the class's construction method to private. This way, you cannot use the new operator to produce objects of the class outside of the class, but you can still produce objects of that class inside the class.
2. Generate a static instance within the class.
3. Provide a static method for externally obtaining instances of this class.
Because the object of the class cannot be obtained outside of the class, only one static method of the class can be called to return the object created inside the class, and because the static method can only access static member variables in the class, the variables that produce the class object inside the class must also be defined as static.
Class chinese{
Static Chinese objRef = new Chinese ();
Private Chinese () {}
public static Chinese getinstance () {
return objRef;
}
}
Class chinese_opt{
static chinese_opt objRef = null;
Private chinese_opt () {}
public static chinese_opt getinstance () {
if (ObjRef = = null)
ObjRef = new chinese_opt ();
return objRef;
}
}
Class testchinese{
public static void Main (string[] args) {
Chinese chinese1 = chinese.getinstance ();
Chinese chinese2 = chinese.getinstance ();
System.out.println (chinese1 = = Chinese2);
}
}
1: Construction method Privatization:
The three main features of object-oriented programming: encapsulation, inheritance, polymorphism. The encapsulation of a class is not only embodied in the encapsulation of attributes, but in fact the method can also be encapsulated, and the method of construction is a special method of course can be encapsulated, for example, the following code is the encapsulation of the construction method:
Package Com.zxf.javaopp;
Public class Singleton {
Private Singleton () {//The construction method is encapsulated, privatized
}
Public void print () {
System. out. println ("Hello world!!!");
}
}
The constructor is privatized, cannot be instantiated externally, cannot be used externally, the object can only be instantiated inside the class, and then used externally, since the class cannot be instantiated externally, the internal instantiation must be decorated with the static keyword: The code is as follows:
Package Com.zxf.javaopp;
class singleton1{
Static Singleton1 Instance = new Singleton1 (); Internally producing instantiated objects of this class
Private Singleton1 () {//The construction method is encapsulated, privatized
}
Public void print () {
System. out. println ("Hello world!!!");
}
}
Public class singletondemo01{
Public Static void Main (String args[]) {
Singleton1 S1 = null ; declaring objects
S1 = Singleton1. instance ; Get instantiated Object
S1.print (); Calling methods
}
}
While the above code implements functionality, there are some problems: usually: We encapsulate the attributes, and the code is modified as follows:
Package Com.zxf.javaopp;
class singleton1{
Static Singleton1 Instance = new Singleton1 (); Internally producing instantiated objects of this class
Public Static Singleton1 getinstance () {//instance object obtained by static method
return instance ;
}
Private Singleton1 () {//The construction method is encapsulated, privatized
}
Public void print () {
System. out. println ("Hello world!!!");
}
}
Public class singletondemo01{
Public Static void Main (String args[]) {
Singleton1 S1 = null ; declaring objects
S1 = Singleton1. getinstance () ; Get instantiated Object
S1.print (); Calling methods
}
}
The code above seems to be complicated, so it's better not to instantiate the constructor method, but why do you do it again?
If we want to produce several objects now: According to the previous code, we need to instantiate several times, each object has its corresponding stack space, at this time the memory consumes a lot, the system efficiency pit is relatively low, but using the construction method encapsulation mode efficiency is relatively high:
Use the method that encapsulates the construction method: Regardless of how many objects are produced, we instantiate only once, and such a design is called a single-state design pattern in design mode (singleton design mode):
Singleton, if you do not want a class to produce multiple instances, you must use a single-state design pattern, which is often used in later high-performance development, and is used extensively in Java's class libraries.
The so-called single-state is the restriction of an object's instance operation at the entrance.
The meaning of the single-state design pattern:
In fact, this mode is very common, we all use the Windows operating system used in this design mode, windows with a back to the station:, in addition to the desktop, each of the other hard disk has a back to the station, the other back to the station and the other hard drive each back station is the same, That is, the entire operating system has only one back to the station, each place just refers to this instance.
Summarize:
The core of a single-state design pattern is the
The constructor is privatized, the instance object is generated inside the class, and the instance object of the class is returned through the static method of the class.
Java single-state design mode