JAVA single-State design mode and JAVA mode

Source: Internet
Author: User

JAVA single-State design mode and JAVA mode

Core: privatize the constructor within the class, generate objects internally, and return the reference of the instantiated object through the class. static Method (static)

 

The design mode is the code result, programming style, and thinking method that is optimized after a large number of practical summaries and theories. the design pattern is like a classical chess set. We use different chess sets for different chess sets.

Single-state design mode: a certain method is adopted to ensure that only one object instance can be generated for a class in the entire software system, and this class only provides one instance method for obtaining its objects.

To implement the single-State mode in java, you only need to perform the following three steps:

1. declare the access permission of the class constructor as private. in this way, the new operator cannot be used to generate class objects outside the class, but the class objects can still be generated within the class.

2. Generate a static instance within the class.

3. Provide a static method for obtaining instances of this class externally.

Because the class object cannot be obtained outside the class, you can only call a static method of the class to return the object created inside the class. Because the static method can only be a static member variable in the category class, the variables that point to the class that generate the class object 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: constructor privatization:

Three main features of object-oriented programming: encapsulation, inheritance, and polymorphism. The encapsulation of classes is not only reflected in the encapsulation of attributes, but actually methods can also be encapsulated. constructor methods can also be encapsulated as special methods, for example, the following code encapsulates the constructor:

PackageCom. zxf. javaopp;

 

Public ClassSingleton {

PrivateSingleton () {// encapsulate and privatize the constructor.

}

Public VoidPrint (){

System.Out. Println ("Hello World !!! ");

}

}

The constructor is privatized and cannot be used externally because it cannot be instantiated externally. In this case, the object can only be instantiated inside the class and then the class can be used externally, since this class cannot be instantiated externally, the internal instantiation must be modified with the static Keyword: the code is as follows:

PackageCom. zxf. javaopp;

 

ClassSingleton1 {

StaticSingleton1Instance=NewSingleton1 (); // an instantiated object of this class is generated internally.

PrivateSingleton1 () {// encapsulate and privatize the constructor

}

Public VoidPrint (){

System.Out. Println ("Hello World !!! ");

}

}

Public ClassSingletonDemo01 {

Public Static VoidMain (String args []) {

Singleton1 s1 =Null; // Declare an object

S1 = Singleton1.Instance; // Get the instantiated object

S1.print (); // CALL THE METHOD

}

}

Although the above Code implements the function, there are some problems: Normally, we encapsulate the attribute, and the code should be modified as follows:

PackageCom. zxf. javaopp;

 

ClassSingleton1 {

StaticSingleton1Instance=NewSingleton1 (); // an instantiated object of this class is generated internally.

Public StaticSingleton1 getInstance () {// obtain the instance object through the static method

Return Instance;

}

PrivateSingleton1 () {// encapsulate and privatize the constructor

}

Public VoidPrint (){

System.Out. Println ("Hello World !!! ");

}

}

Public ClassSingletonDemo01 {

Public Static VoidMain (String args []) {

Singleton1 s1 =Null; // Declare an object

S1 = Singleton1.GetInstance(); // Get the instantiated object

S1.print (); // CALL THE METHOD

}

}

The above Code seems to be complicated. It is better not to instantiate the constructor, but why?

If we want to generate several objects now: according to the previous code, we need to instantiate them multiple times. Each object has its corresponding stack space, which consumes a lot of memory at this time, the efficiency of the system is relatively low, but the efficiency of the mode encapsulated by the constructor is relatively high:



The method encapsulated using the constructor: no matter how many objects are generated, We instantiate them only once. Such a design is called the single-State design mode (Singleton design mode) in the design mode ):

SingletonIf you do not want a class to generate multiple instances, you must use the single-State design mode, which is frequently used in high-performance development in the future, this design mode is widely used in java class libraries.

The single State restricts the instance operations of objects at the entrance.

Significance of the single-State Design Model:

In fact, this mode is very commonly used. We use this design mode in the windows operating system. In windows, we use a back-to-station: apart from the desktop, each of the other hard disks has a return station. The other return stations and each return station on the other hard disks are the same, that is, there is only one return station in the entire operating system, the instance is referenced in each region.

Summary:

The core of the single-State design mode is

The constructor is privatized. instance objects are generated inside the class, and instance objects of the class are returned through static methods of the class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.