Design Mode-singleton mode (2) Registration Mode

Source: Internet
Author: User

This is the second article on the design mode study notes. It mainly analyzes the singleton mode. Including lazy, hungry, registered, and improved lazy,
There is also an instance for reading the propertoes configuration file. Now it seems that four sections are required. This is the second section. The last section analyzes the basic lazy and ELE. Me types,
Let's take a look at the registration form this time.


The actual maintenance of a group of Singleton modes in the registration mode is mainly the expansion of quantity. We store Singleton through map, so that when calling, first, determine whether the Singleton has been created. If yes, return directly. If not, create a register to map and then return. The quantity is divided into fixed quantity and unfixed quantity. The following uses an unfixed number of methods. In the getInstance method, add the parameter (string name ). Then, use subclass inheritance to overwrite this method to pass the name. Let's look at the code.


The first is the parent class:

[Java]
// Use Map to configure multiple Singleton instances
Public class MySingleton3 {

// Set static variables to directly create an instance
Private static Map <String, MySingleton3> map = new HashMap <String, MySingleton3> ();

// ----- Protected ----- constructor, which cannot be private, but the subclass can directly access the constructor.
// The solution is to put your Singleton class in an external package so that classes (including the default package) in other packages cannot instantiate a singleton class.
Protected MySingleton3 (){
System. out. println ("--> private constructor called, creating instance ");

}
 
// Open a public method to check whether an instance exists and returns. If no public method is created
Public static MySingleton3 getInstance (String name ){
If (name = null ){
Name = MySingleton3.class. getName ();
System. out. println ("--> name does not exist. name value assignment equals to" + MySingleton3.class. getName ());
}
If (map. get (name) = null ){
Try {
System. out. println ("--> the value corresponding to name does not exist. Start to create ");
Map. put (name, (MySingleton3) Class. forName (name). newInstance ());
} Catch (InstantiationException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
} Else {
System. out. println ("--> the value corresponding to name exists ");
}
System. out. println ("--> return the value of name ");
Return map. get (name );
}

Public Map <String, MySingleton3> getMap (){
Return map;
}
}

Then two subclasses are written:
[Java]
Public class MySingleton3Childa extends MySingleton3 {

Public static MySingleton3Childa getInstance (){
Return (MySingleton3Childa) MySingleton3Childa
. GetInstance ("com. xq. mysingleton. MySingleton3Childa ");
}
 
// Write a test method at will
Public String about (){
Return "----> I am MySingleton3's first subclass MySingleton3Childa ";
}
}

[Java]
Public class MySingleton3Childb extends MySingleton3 {

Static public MySingleton3Childb getInstance (){
Return (MySingleton3Childb) MySingleton3Childb
. GetInstance ("com. xq. mysingleton. MySingleton3Childb ");
}
 
// Write a test method at will
Public String about (){
Return "----> I am the second subclass of MySingleton3 MySingleton3Childb ";
}
}


Then there is the client code. This test case compares Doha:

[Java]
/**
* Registration Singleton Mode
* MySingleton2
*/
Public static void myprint3 (){
System. out. println ("------------------- registered Singleton mode ----------------");
System. out. println ("First Instance acquisition (registration type )");
MySingleton3 s1 = MySingleton3.getInstance (null );
System. out. println (s1 );
System. out. println ("second instance acquisition (registration type )");
MySingleton3Childa s3 = MySingleton3Childa. getInstance ();
System. out. println (s3 );
System. out. println (s3.about ());
System. out. println ("the third instance (registration type )");
MySingleton3Childb s4 = MySingleton3Childb. getInstance ();
System. out. println (s4 );
System. out. println (s4.about ());
System. out. println ("the fourth time the instance is obtained (the constructor of the new subclass is not normal )");
MySingleton3Childb s5 = new MySingleton3Childb ();
System. out. println (s5 );
System. out. println (s5.about ());
System. out. println ("output all the singleton stored by Map in the parent class. It can be seen that the new instance does not exist in Map ");
System. out. println (s1.getMap ());
System. out. println ();
}
 
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
// Lazy
// Myprint ();
// Hungry Chinese
// Myprint2 ();
// Lazy Improvement
// Myprint2a ();
// Registration type
Myprint3 ();
 
}

 

Let's take a look at the output:

----------------- Registration-type Singleton mode ----------------
First instance acquisition (registration type)
--> The name does not exist. The value of name is equal to com. xq. mysingleton. MySingleton3.
--> The value corresponding to name does not exist. Start to create
--> The private constructor is called to create an instance.
--> Returns the value of name.
Com. xq. mysingleton. MySingleton3 @ 6e1408
Second Instance acquisition (registration type)
--> The value corresponding to name does not exist. Start to create
--> The private constructor is called to create an instance.
--> Returns the value of name.
Com. xq. mysingleton. MySingleton3Childa @ f62373
----> I am the first subclass of MySingleton3, MySingleton3Childa.
Third Instance acquisition (registration type)
--> The value corresponding to name does not exist. Start to create
--> The private constructor is called to create an instance.
--> Returns the value of name.
Com. xq. mysingleton. MySingleton3Childb @ 1f33675
----> I am the second subclass of MySingleton3, MySingleton3Childb
Obtain the instance for the fourth time (the constructor of the new subclass is abnormal)
--> The private constructor is called to create an instance.
Com. xq. mysingleton. MySingleton3Childb @ 7c6768
----> I am the second subclass of MySingleton3, MySingleton3Childb
Output all the singleton stored by Map in the parent class. It can be seen that the new instance does not exist in Map.
{Com. xq. mysingleton. mySingleton3Childa = com. xq. mysingleton. mySingleton3Childa @ f62373, com. xq. mysingleton. mySingleton3Childb = com. xq. mysingleton. mySingleton3Childb @ 1f33675, com. xq. mysingleton. mySingleton3 = com. xq. mysingleton. mySingleton3 @ 6e1408}


 

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.