JAVA course 10 (Singleton design mode)

Source: Internet
Author: User

JAVA course 10 (Singleton design mode)

Miscellaneous:

If the methods in a class are static, there is no need to create objects. To prevent other programs from creating objects, You Can privatize the constructors of the current class.

class MAN{private MAN(){}}

Document Note: Command: javadoc

Only/** start to */end content can be parsed;/**/No

Path setting problems:

When you want to run a class file in more than two paths, you must set the path. Command: classpath =.; c: \ myhelp or: classpath =.; % myhelp %

Clear Path: set classpath =


Design Model: an effective solution to the problem

Singleton design mode:

Ensure the uniqueness of a class in the memory.

When multiple programs use the same configuration information object, the uniqueness of the object must be guaranteed.

How to Ensure the uniqueness of objects:

1. Do not allow other new objects
2. Create an instance of this class in this class
3. provide a method for other programs to obtain the object.

Steps:

1. privatize the constructor of this class
2. Create a class object in this class through new

3. define a common method to return the created object

Import java. util. strong; class MAN {private static MAN jo = new MAN (); // Step 2 // not all, for controllable private int x = 5; private MAN () // Step 1 {} public static MAN Creat (String key) // step 3 {if (key = "stdio") // The object is used correctly, otherwise, do not return jo; return null;} void show () {System. out. println ("ASDf") ;}} public class Main {public static void main (String [] args) {// man ss = MAN. creat (); String key = "stdio"; MAN s = MAN. creat (key );}}


The first mode of Singleton Design

// Hungry Chinese

Example:
Import java. util. exist; class Test {private static Test jo = new Test (); // step 2, // when the class is loaded, the object exists // not shared, to control private int num; private Test () // Step 1 {} public static Test Creat (String key) // step 3 {if (key = "stdio ") // if it is correct, the object will be used. Otherwise, the return jo; return null;} public void set (int num) {this. num = num;} public int get () {return num;} void show () {System. out. println ("ASDf") ;}} public class Main {public static void main (String [] args) {String key = "stdio"; Test s1 = Test. creat (key); Test s2 = Test. creat (key); System. out. println (s1 = s2); s1.set (10); s2.set (20); System. out. println (s1.get () + "" + s2.get (); // 20, because there is only one object }}

Object Memory created in the class:



Another manifestation of the singleton design model:

// Lazy. It can be used only when it is actually used. Features: delayed Loading Mode

Class Test {private static Test s = null; // The class is loaded only when the Creat method is called and the Creation conditions are met, the object will be created // that is, the singleton design mode and the delayed loading mode. Generally, the Test point is private int num; private Test () {} public static Test Creat (String key) {if (key = "stdio" & s = null) s = new Test (); // if yes, return s to the object ;} public void set (int num) {this. num = num;} public int get () {return num;} void show () {System. out. println ("ASDf") ;}} public class Main {public static void main (String [] args) {String key = "stdio"; Test s1 = Test. creat (key); Test s2 = Test. creat (key); System. out. println (s1 = s2 );}}

PS


During the interview, the interview is generally lazy.
Use the hungry Chinese style during development

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.