Singleton (Creation Mode) (4)

Source: Internet
Author: User

If there is a Person class in the program, my requirement is that only one Person can be created in the whole application, and this Person instance is shared in the application, how should we implement it? Step 1: Create a Person class. In the class, we will privatize the constructor, so that we can no longer create an external new one. Step 2: we can obtain the Person instance externally by exposing a Person attribute instance or getting the Person instance. OK, check the following code: the code of the singleton mode is as follows. java copy code package com. designpattern. singleton; public class Person {private String name; private int age; // hungry Chinese public static final Person PERSON = new Person (); private Person () {} public String getName () {return name;} public void setName (String name) {this. name = Name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} copy the code MainClass. java copy code package com. designpattern. singleton; public class MainClass {public static void main (String [] args) {// if I want to have only one Person in the entire application, that is, only new Person () can be created once, there can only be one Person instance/** Singleton mode. First implementation: ELE. Me * // Person person = Person. PERSON; // person. setName ("jack"); // Person person2 = Person. PERSON; // perso N2.setName ("jack1"); // System. out. println (person. getName (); // System. out. println (person2.getName (); // The name of the result is the same. All of them are in jack1/*** Singleton mode. Method 2: lazy, this method is insecure under multiple threads * // Person2 person = Person2.getPerson2Instance (); // person. setName ("jack"); // Person2 person2 = Person2.getPerson2Instance (); // person2.setName ("jack1"); // System. out. println (person. getName (); // System. out. println (person2.getName (); // The results have the same name and are all j Ack1/** Singleton mode third implementation: Dual verification, multi-thread security */Person2 person = Person2.getPerson2Instance2 (); person. setName ("jack"); Person2 person2 = Person2.getPerson2Instance2 (); person2.setName ("jack1"); System. out. println (person. getName (); System. out. println (person2.getName (); // The name of the result is the same. All of them are in the example mode of jack1. designpattern. singleton; public class Person2 {private String name; Private int age; private static Person2 person2; private Person2 () {} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;}/*** single thread obtains a single person2 instance * @ return */public static Person2 getPerson2Instance () {if (person2 = null) {person2 = new Person2 ();} return person2 ;}/*** Obtain a single person2 instance with multiple threads * @ return */public static Person2 getPerson2Instance2 () {if (person2 = null) {synchronized (Person2.class) {if (person2 = null) {person2 = new Person2 () ;}} return person2 ;}} copy Code 1. What is Singleton mode? Singleton mode is an object creation mode, the Singleton mode ensures that only unique instance objects are generated for a class. That is to say, in the whole program space, the class only has one instance object. In fact, the GoF defines the singleton mode as: to ensure that only one class exists in one instance, and to provide a global access method that can access this instance. 2. Why is the singleton mode used? In application system development, we often have the following requirements:-between multiple threads, such as the servlet environment, share the same resource or operate on the same object-use global variables in the whole program space, share resources-in a large-scale system, you need to save the object creation time for performance considerations, and so on. Because the Singleton mode can ensure that only unique instance objects are generated for a class, the Singleton mode will be used in these cases.

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.