"Design pattern Refinement" Learning notes (12)------Singleton (single piece) mode

Source: Internet
Author: User

"Design pattern Refinement" Learning notes (12) ------Singleton (single piece) mode

GoF : guarantees that a class has only one instance and provides a global access point to access it.

The singleton model is relatively simple.

For performance reasons, I might not want to instantiate these objects repeatedly and then destroy them again. And even though I can instantiate all of the possible policy objects at the beginning, this approach is getting lower if the number of policies becomes larger and bigger. Instead, the best approach is to instantiate when needed, but only once.

The singleton mode works by having a particular method that is used to instantiate the object that is needed.

L When this method is called, it checks to see if the object has been instantiated. If the object has already been instantiated, this method simply returns a reference to that object. If the object has not been instantiated, this method instantiates the object and returns a reference to the new instance.

L to make sure that this is the only way to create objects of this type, I define constructors as protected or private in this class.

The biggest use of the singleton pattern is that it allows us to instantiate only one object at a time without requiring the client object to be concerned about the existence of the object.

The following is a UML diagram of the singleton schema:

A simple example is given below:

Package Singleton;

public class Singleton

{

private int sing = 0;

Private Singleton ()//provides a proprietary constructor to prevent inheritance

{

Do something ...

}//end Singleton ()

private static Singleton instance = new Singleton ();

public void Printmessage ()

{

System.out.println ("Singleton pattern.") ");

sing++;

System.out.println ("sing =" + sing);

}//end Printmessage ()

public static Singleton getinstance ()

{

return instance;

}//end getinstance ()

}//end class Singleton

Package Singleton;

public class Singletonpattern

{

Private Singleton SL;

Private Singleton SL2;

Private Singleton SL3;

Private Singleton SL4;

public void Singletontest ()

{

SL = Singleton.getinstance ();

SL2 = Singleton.getinstance ();

SL3 = Singleton.getinstance ();

SL4 = Singleton.getinstance ();

Sl.printmessage ()//to implement synchronous mutex

Sl2.printmessage ();

Sl3.printmessage ();

Sl4.printmessage ();

}//end Singletontest ()

public static void Main (string[] args)

{

System.out.println ("The Singleton pattern!");

Singletonpattern sp = new Singletonpattern ();

Sp. Singletontest ();

}//end Main (...)

}//end class Singletonpattern

The following are the results of the operation:

The Singleton pattern!

Singleton pattern.

Sing = 1

Singleton pattern.

Sing = 2

Singleton pattern.

Sing = 3

Singleton pattern.

Sing = 4

There are several forms of the Singleton pattern: the Singleton class above is one. The following is given a:

public class Singleton

{

private static Singleton instance = NULL;

public static synchronized Singleton getinstance ()

{

if (instance = null)

Instance = new Singleton ();

return instance;

}//end getinstance ()

}//end class Singleton

A lot of information on the internet, according to Daniel said the first method in Java is more secure. I thought about it but I didn't fully understand it. There is no room for comment.

Singleton mode also has a variant: called double-checked locking (DCL) is used for multithreading. There is a lot of controversy online about this variant, and I'm not making any comments here. Maybe it's not enough, and it's probably just too limited, and I can't think of a singleton mode in the program I've done before. So the understanding of the singleton mode is not very profound, it can be said to be just an epidermis. Anyway, I'll look back later. Now let's get to the point here. Also please master a lot of advice.



trackback:http://tb.blog.csdn.net/trackback.aspx?postid=611868

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.