Private construction Method--reproduced in Gu Wenren

Source: Internet
Author: User

Before you explain this operation, first observe the following procedure.

Class Singleton {//define one of the classes

public void print () {

System.out.println ("Hello World");

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = new Singleton (); Instantiating an Object

Inst.print (); Calling methods

}

}

In the above program, there is a constructor method in the singleton class (because if a class does not explicitly define a construction method, it will automatically generate a non-parametric, do nothing construction method), but the following to change the construction method.

Class Singleton {//define one of the classes

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

}

It is now found that when instantiating the Singleton class object, the program has a compile error because the construction method is privatized and cannot be called externally, that is, the object of the Singleton class cannot be instantiated externally.

So now it's time to think: how can you get outside of a class to invoke the print () method by instantiating an object if the construction method in the Singleton class is not modified and the print () method is not modified?

Think process one: the operation defined with private access permission can only be accessed by this class, external cannot be called, then now that the construction method has been privatized, it proves that the constructor of this class can only be called by this class, namely: now in this class to produce the class instantiation object.

Class Singleton {//define one of the classes

Singleton instance = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

}

Thinking process two: for the ordinary properties of a class, by default must be in this class to have instantiated objects before the call can be made, but now in the outside of the Singleton class cannot produce instantiated objects, then you must think of a method, The instance property in the singleton class can be called when there is no Singleton class instantiation object, can be done with static, the property characteristics of the static definition: called directly by the class name, and can be called when the object is not instantiated.

Class Singleton {//define one of the classes

Static Singleton instance = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = singleton.instance; Instantiating an Object

Inst.print (); Calling methods

}

}

Thinking process three: all the properties in the class should be encapsulated, so the above instance property should be encapsulated, and after encapsulation to get the property to write Getter method, but this time the getter method should also be called directly by the class name, defined as the static type.

Class Singleton {//define one of the classes

private static Singleton instance = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

public static Singleton getinstance () {

return instance;

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = Singleton.getinstance (); Instantiating an Object

Inst.print (); Calling methods

}

}

Thinking process four: The purpose of doing this? At this time the instance property in the program, which belongs to Static, then represents all Singleton class objects, no matter how many have the same instance property together, then since there is the same, then what is the point?

Now do a simple thinking: If a class now only want to have a unique instantiation of the object appears, you should control the construction method, if the construction method is not visible to the outside, then it is certainly not possible to perform the object instantiation, you must hide the construction method, using private hidden.

Since this is clear, there is still a problem with this procedure.

public static Singleton getinstance () {

Instance = new Singleton ();

return instance;

}

There is no error in this operation syntax, and there is no need to consider whether it makes sense, but now the code is allowed to do so, and doing so finds that all efforts to represent the only instantiated object are wasted, so you have to think of ways to abolish this practice and add a final keyword when defining instance.

Class Singleton {//define one of the classes

private static final Singleton INSTANCE = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

public static Singleton getinstance () {

return INSTANCE;

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = Singleton.getinstance (); Instantiating an Object

Inst.print (); Calling methods

}

}

Such a design is called a singleton design pattern (Singleton) in design mode.

Interview question: Please write a singleton program, and explain its main features?

Class Singleton {//define one of the classes

private static final Singleton INSTANCE = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

public static Singleton getinstance () {

return INSTANCE;

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = Singlet

Inst = Singleton.getinstance (); Instantiating an Object

Inst.print (); Calling methods

}

}

Such a design is called a singleton design pattern (Singleton) in design mode.

Interview question: Please write a singleton program, and explain its main features?

Class Singleton {//define one of the classes

private static final Singleton INSTANCE = new Singleton ();

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

public static Singleton getinstance () {

return INSTANCE;

}

}

public class Test {

public static void Main (String args[]) {

Singleton inst = null; declaring objects

Inst = Singleton.getinstance (); Instantiating an Object

Inst.print (); Calling methods

}

}

Features: The construction method is privatized, only through the getinstance () method to obtain the instantiation of the Singleton class object, so no matter how the external operation, and finally only one instantiation object, in the case of a single design pattern, there will be a static method, Used to get the instantiated object of this class.

For a singleton design pattern, this concept is only used in future development, but the specific code is seldom written.

Extension (can not):

There are two types of singleton design patterns in terms of design patterns:

· A hungry Man: the previously written program belongs to the A hungry man type, because the Insntace attribute in the class is instantiated directly when the attribute is defined;

· Lazy: Instantiate an object The first time it is instantiated with a class.

Example: Observing lazy-looking

Class Singleton {//define one of the classes

private static Singleton instance;

Private Singleton () {}//Construction method privatization

public void print () {

System.out.println ("Hello World");

}

public static Singleton getinstance () {

if (instance = = NULL) {//No instantiation

Instance = new Singleton (); Instantiation of

}

return instance;

}

}

These concepts are clear, for the single-case design again emphasize: Remember the structure of the Code and the characteristics of the operation, although this code will not be written by you, but the concept will certainly be used.

Reprint Address: http://www.cnblogs.com/guwenren/archive/2013/04/12/3016489.html

Private construction Method--reproduced in Gu Wenren

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.