Design Pattern: Singleton
The Singleton pattern ensures that class has only one instance and provides that instance a global point of access. The following two classes, IdentityValidationStrategyParser and IdentityValidationStrategyFactory, are implemented by using the Singleton Pattern so as to provide a global access point to the instance. This access point parses the identity validation configuration XML file, which then maps it to the validation strategy Java objects and stores them into the list of identityValidationStrategies and its corresponding java.util.HashMap instance(validationStrategyMap).
To improve multithreading performance, use the "double-checked locking" mechanism to reduce the use of synchronization in get unique instance methods, such as getUniqueParser() (in the IdentityValidationStrategyParser) and getUniqueFactory() (in IdentityValidationStrategyFactory). The following code demonstrates:
/**
* Get the unique instance of the IdentityValidationStrategyParser
*
* @return IdentityValidationStrategyParser
*/
public static IdentityValidationStrategyParser getUniqueParser() {
if (uniqueParser == null) {
// Now the following code execution is only synchronized
// at the first time through
synchronized (IdentityValidationStrategyParser.class) {
if (uniqueParser == null) {
uniqueParser =
new IdentityValidationStrategyParser();
}
}
}
return uniqueParser;
}
You should also use the "volatile" keyword to ensure that multiple threads handle the unique instance variable correctly when it's being initialized to the Singleton instance:
private volatile static IdentityValidationStrategyParser uniqueParser;
It's unfortunate that in Java 1.4 and before, many JVMs contain implementations of the volatile keyword that allow improper synchronization for "double-checked locking." Of course, you can also create an instance of IdentityValidationStrategyParser in a static initializer, like this:
private static IdentityValidationStrategyParser uniqueParser
= new IdentityValidationStrategyParser();
…..
public static IdentityValidationStrategyParser getUniqueParser() {
return uniqueParser;
}
This code is guaranteed to be thread safe. You rely on the Java JVM to guarantee the creation of an instance of IdentityValidationStrategyParser before any thread accesses the static uniqueParser variable.
The New Class Design Diagram
Figure 8 shows the class diagram for the Personal Identity Validator implementation.

Figure 8. The New Class Diagram
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.