Singleton design pattern is one of the mostly used design pattern. This tutorial helps you to add a template in Eclipse to easily create Singleton Java classes.
Step 1:
Go to the Windows → Preferences
Step 2:
Expand the Java → Editor → Templates as shown below.
Step 3:
Click on the “New...” button.
In the appeared dialog, provide the following details.
Name: singleton
Description: Singleton design pattern
Pattern:
/**
* static Singleton instance.
*/
private static volatile ${enclosing_type} instance;
/**
* Private constructor for singleton.
*/
private ${enclosing_type}() {
}
/**
* Return a singleton instance of ${enclosing_type}.
*/
public static ${enclosing_type} getInstance() {
// Double lock for thread safety.
if (instance == null) {
synchronized(${enclosing_type}.class){
if (instance == null) {
instance = new ${enclosing_type}();
}
}
}
return instance;
}
Step 5:
Apply the changes and open any Java class to which you want to apply the singleton design pattern.
Step 6:
Type singleton and press Ctrl + Space.
Step 7:
Select the template by pressing enter or double clicking on it.
I hope it helps you to save your time in creating singleton classes. If you have any problems, feel free to comment below.
2 comments
Write commentsasfasfasfasf
ReplyTheir team’s transparent and supportive approach supplemented the UX design companies engagement. Customers can expect a communicative team of problem-solvers.
ReplyEmoticonEmoticon