Java Design Pattern Learning 1 -- Singleton pattern [original]

Source: Internet
Author: User

I wrote a database connection pool with some reference to some materials and admired the practice of generating only one instance. At noon today, I heard someone talking about the design mode and couldn't stand it anymore, the Internet goes down.
As a result, we can see the singleton mode. As a result, isn't that the method used in the connection pool ?! Oh, it turns out so.
Singleton is a creation model that ensures that only one instance is generated and provides a global access point to access it.
To implement the singleton mode, we need a static variable to remember whether an instance has been generated without creating an object. Static variables or static methods can be called directly without generating a specific instance. Such variables or methods will not change because of class instantiation.
There are usually three ways to implement the singleton mode:
1. Use static methods to implement Singleton;
2. Implement Singleton with static variables as the sign;
3. Use the register mechanism to create Singleton.
For the above three methods, I think the first method is very useful, and the write connection pool uses the first and third methods. Use the first method to make sure that the connection pool exists. Use the third method to make sure that the connection pool exists.
The first method is as follows:
Declare the constructor as private to avoid creating an instance in any way other than the methods we provide. If you do not declare the constructor as private, the compiler automatically synchronizes a default friendly constructor. The following is an example of excerpt:
Public class Singleton
{
Private Static Singleton S;
Private Singleton ()
{
};
  
Public static Singleton getinstance ()
{
If (S = NULL)
S = new Singleton ();
Return S;
}
}

// Test class
Class singletontest
{
Public static void main (string [] ARGs)
{
Singleton S1 = singleton. getinstance ();
Singleton S2 = singleton. getinstance ();
If (S1 = S2)
System. Out. println
("S1 is the same instance with S2 ");
Else
System. Out. println
("S1 is not the same instance with S2 ");
}
}
  
The running result of singletontest is:
  
S1 is the same instance with S2
  
This proves that we have created only one instance and it is very simple and practical.

For the second implementation method, a static variable is added to the class as a sign and each time it is checked in the constructor.
The following is an example:
Public class Singleton {
Static Boolean existinstance = false;
Public Singleton ()
{
If (existinstance)
{
System. Out. println ("already exist instanse! ");
}
Else
{
//...........................
System. Out. println ("Create an instanse! ");
Existinstance = true;
}
}
}
The test class is as follows:
Public class test {

Public static void main (string [] ARGs ){
Singleton S1 = new Singleton ();
Singleton S2 = new Singleton ();
}
}
Result:
Create an instanse!
Already exist instanse!
The second time I detected that an instance already exists, I will check what I want to do next.
Compared with the first method, the first method is whatever you want to do. The second method is to generate an instance for the first time and then report an error.
Next let's take a look at the third method: Use the register mechanism to create Singleton.
In this method, you must first create a manager and use hashtable and enumeration in the set to implement additem (Object key, object value), getitem (Object Key), removeitem (Object Key), and other methods, associate keys and values one by one. Before creating an instance, register the instance using the additem method and obtain the instance using the getitem method.
The key in hashtable is unique to ensure that the created instance is unique. The advantage of using the register mechanism to create Singleton mode is that it is easy to manage and can control multiple Singleton instances of different types at the same time.
Now, let me get some code from the connection pool and talk about the third method:
First, define a static hashtable:
Public static hashtable pools = new hashtable ();
When the connection pool is successfully created, put the generated connection pool object in pools:
Dbconnectionpool pool = new dbconnectionpool (poolname, URL, USR, PWD, max); (dbconnectionpool is a custom connection pool object)
Pools. Put (poolname, pool );
The following code is used to obtain the connection:
Public connection getconnection (string poolname)
{
Dbconnectionpool pool = (dbconnectionpool) pools. Get (poolname );
If (pool! = NULL)
{
Return pool. getconnection ();
}
Return NULL;
}
As mentioned above, the key value in hashtable is unique. In this way, only one connection pool named poolname can exist.
  
I have finished talking about it, and I have no idea what to say .......

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.