A single-instance design pattern for Java design patterns

Source: Internet
Author: User

Design pattern: The most effective way to solve a certain kind of problem.

23 Design Patterns in Java.

Singleton design Pattern: Solving a class that only has one object in memory.

The Runtime () method is designed in a single-instance design pattern.

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 is unique in memory.

1, in order to avoid other programs to set up such objects too much, first prohibit other programs to establish such objects.

2. To allow other programs to access the class object, you have to customize an object in this class.

3, in order to facilitate other programs on the custom object access, can provide some external access.

Steps:

1, because the creation of objects requires constructor initialization, as long as the constructors in this class are privatized, other programs can no longer create the class object;

2, create an object of this class in a class;

3, define a method that returns the object so that other programs can get this type of object by means of the method. (Effect: controllable).

How to describe things and how to describe them.

Add the above three steps when you need to guarantee that the object of the thing is unique in memory.

a hungry man: A single class enters the object, it has already created the object.

Lazy Type: The single class enters memory, the object does not exist, and the object is created only when the GetInstance method is called

The first example of a single design pattern

This is the first object to initialize. Called: a hungry man type.

Once the single class is in memory, the object has been created.

Class single

{

private static Single=new single ();

Private single () {}

public static single getinstance ()

{

return single;

}

}

A Hungry man type:

The second method of designing a single case

An object is a method that is called when it is initialized, also called an object's lazy load, known as: the Idle type.

Single class into memory, the object does not exist, only the object is created when the GetInstance method is called.

Class single

{

private static single s=null;

Private single () {}

public static single getinstance ()

{

if (s==null)

{

Synchronized (Single.class)

{

if (s==null)

S=new single ();

}

}

return s;

}

}

lazy in the multi-threaded access will create a security risk, at this time can join the synchronization function to solve security problems .

A single-instance design pattern for Java design patterns

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.