Dark Horse Programmer--java Basics-Object Oriented 3

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

I. Overview
1. Design mode
Problem solved: Ensure that the object uniqueness of a class in memory.
For example: When multiple programs read a configuration file, it is recommended that the configuration file be encapsulated as an object, it is convenient to manipulate the data, and to ensure that multiple programs read the same profile object, it is necessary that the configuration file object in memory is unique.
2. How to ensure the uniqueness of the object?
Thought:
1. Do not allow other programs to create the object
2. Create an object of this class in this class
3. Provide external methods for other programs to obtain this object
Steps:
1. Privatize the constructor
2. Create a class object in the class
3. Define a method that returns the object so that other programs can obtain the object by means of the method.
Two: Realize

//a hungry Man type public class Singledemo{public static void Main (String args[]) {Single SS = Single.getinstance ();}} Class Single{private single () {}//privatisation constructor private static single S = new single ();//create Private and static this class object public static single Getinsta NCE () {//defines public and static methods, returns the object return s;}} Lazy public class Singledemo{public static void Main (String args[]) {Single SS = Single.getinstance ();}} Class Single{private single () {}private static single s = null;//first initialized to nullpublic static single getinstance () {if (s = = null) { Determine if NULL s = new single ();//Create a new class return s;}} Lazy type will be the problem, that is, after the previous object entered, the CPU interrupted the operation of his, the future will not go to the solution: Class Single{private single () {}private static single s = null;// First initialized to nullpublic static synchronized single getinstance () {//with synchronized lock if (s = = null) {//To determine if null s = new single ();// Create a new class return s;}} But it's inefficient to do so. Class Single{private static single S = Null;private single () {}public static single getinstance () {if (s==null) { Synchronized (Single.class) {if (s==null)//double-judge S = new single ();}} return s;}} 

Three: Create an object in detail
Person p = new person ();
What does the creation object do in memory?
1. Load the Person.class file of the specified location on the hard disk into the memory first
2. When the main method is executed, the space of the main method is opened in the occupied memory, and then a variable p is assigned to the stack area of the main method.
3. Open an entity space in the heap memory and allocate a memory's first address new
4. Spatial allocation of attributes in the entity space and default initialization
5. Display initialization of properties in space
6. Construct code block initialization for an entity
7. Call the constructor for the entity to initialize the constructor function.
8 . Assign the first address to the P,P variable to reference the entity (that is, point to the object)

Dark Horse Programmer--java Basics-Object Oriented 3

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.