Before Java1.5, there were two ways to implement a singleton (Example 1, Example 2), but the third method (Example 4) that became possible in 1.5 is the best.
A type of design pattern. Sometimes called the "Singleton pattern". In short, a class that is instantiated exactly once. It is used when you want to realize the state that "there is only one instance of a certain class at any time".
How to make a singleton -Declare the constructor as private. (To prevent the constructor from being called from the side that uses this class)
-Declare the method getInstance () to get an instance as static. This allows the user to get an instance with a call to Singleton # getInstance ().
Singleton sample
/**
*Application-wide configuration information
*/
public class Config {
/**
*Declare the constructor private so that it cannot be instantiated externally
*/
private Config() {
//Actually, the setting information is obtained from the file database, etc.
//Write the process to read here
}
/**
*Returns only instance
* @return The only instance of this class
*/
public static Config getInstance() {
return ConfigInstanceHolder.INSTANCE;
}
/**
*Returns the setting value corresponding to the specified key
* @param key setting key
* @return setting value
*/
public String getValue(String key) {
//Actually, the setting information read by the constructor
//Write the process to return here
return ....;
}
/**
*An inner class that holds the only instance of the Config class
*/
public static class ConfigInstanceHolder {
/**Only instance*/
private static final Config INSTANCE = new Config();
}
}
Enum (enumeration type) is a type that can combine multiple constants into one. The constant defined by Enum is called an enumeration.
public class Main {
public static void main(String[] args) {
Fruit fruit_type = Fruit.Orange;
System.out.println(fruit_type);
}
protected enum Fruit {
Orange,
Apple,
Melon
};
}
Execution result
Orange
Elvis Presley
Great rock'n'roller I like "A Little Less Conversation" https://www.youtube.com/watch?v=Zx1_6F-nCaw
Elvis has left the Building.
It is an expression related to the famous episode of the popular singer Elvis Presley of the past. Even after Presley's concert, he started by announcing "Elvis has already left this building" to the audience who are reluctant to return in anticipation of an encore, urging the audience to leave the venue. ..
Currently, it is used for the end of any event.
The constructor is preavte, which provides public static members so that users of the class can access the only instance of the class.
The private constructor is called only once to initialize the public static final field Elvis.INSTANCE. This guarantees that Elvis is the only one in the world.
Example 1
//Singleton by public final field
public class Elvis{
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuliding() { ... }
}
The second method is to make the member of that 1 public static. Returns an object reference with Elvis.getInstance (). The good thing about this is that the declaration makes it clear that the class is a singleton.
Example 2
//Singleton with static factory method
public class Elvis{
private static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public static Elvis getInstance() { return INSTANCE; }
public void leaveTheBuilding() { ... }
}
However, there is a possibility that a fake exists in Elvis created by the methods of Examples 1 and 2, and it is necessary to add the following method to prevent it.
Example 3
//ReadResolve method to preserve singleton characteristics
private Object readResolve() throws ObjectStreamException{
/*
*Return the real Elvis and turn the fake Elvis into a garbage collector
*Get rid of it.
*
*/
return INSTANCE;
}
The third method that has been available since Java 1.5. Simply create an enum type with a single element. It's a simple description, and there are iron walls that don't allow fake, so if you want to make a singleton, use this method.
Example 4
//enum singleton-Preferred method
public enum Elvis {
INSTANCE;
public void leaveTheBuilding() { ... }
}
[Read Effective Java] Chapter 2 Item 4 "Force non-instantiation with a private constructor" https://qiita.com/Natsukii/items/661b645482d814b73914
Recommended Posts