Single-Column mode commonly used in JAVA and android, single-column mode in JAVA and android

Source: Internet
Author: User

Single-Column mode commonly used in JAVA and android, single-column mode in JAVA and android

In many development projects, in order to save resources, the constructor of a class is privatized, so that multiple instances cannot be created in the entire project. This method is called the singleton mode.

The following Code introduces the singleton mode:

After creating a java project, create an entity class User. java, and a test class, main. java

The Code is as follows:

1 public class User {2 private static User user; 3 4 // Add the instance property 5 private String name; 6 private String sex; 7 private String age; 8/** 9 * @ return10 * Initialize an instance 11 */12 public static User getInstens () {13 if (user = null) {14 user = new User (); 15} 16 return user; 17} 18 19/** 20 * to implement the entire project, there is only one instance of this object (which is conducive to resource saving ), so the constructor 21 * of the instance is private. In this way, an instance 22 */23 private User () {24 25} 26 27 public User getUser () cannot be created elsewhere () {28 return user; 29} 30 public void setUser (User user) {31 this. user = user; 32} 33 public String getName () {34 return name; 35} 36 public void setName (String name) {37 this. name = name; 38} 39 public String getSex () {40 return sex; 41} 42 public void setSex (String sex) {43 this. sex = sex; 44} 45 public String getAge () {46 return age; 47} 48 public void setAge (String age) {49 this. age = age; 50} 51 52}

Then add the main code to test:

1 public class main {2 public static void main (String ages []) {3 4 User user = User. getInstens (); 5 user. setName ("Single-Column mode"); 6 System. out. println (user. getName (); 7} 8}

Here, we need main. java to get the User's instance object through User. getInstens.

The whole project ensures that the instance is created only once and used multiple times. Therefore, it is called the singleton mode.

Original Copyright All rights reserved for all http://www.cnblogs.com/taofudemo/p/4621817.html

Reprinted please note

 

Related Article

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.