<!-Description the beginning and outline-> Deep dive into the following content that appears in Effective Java 2nd Edition P6.
The fact that the> static factory method can return the same object no matter how many times it is called can also be used to tightly control what instances exist at any given time. The class that does this control is said to be ** instance-controlled ** (instance-controlled).
<!-Edit title and anchor name->
--The execution environment limits the number of instances of a certain class
--There may be one or more, but ** "There can be no more than n instances of this class !!" **.
--Only one instance exists
--If the instances are the same (ʻa == b), the contents of the instances are also the same (ʻa.equal (b)
).
--Due to the above properties, it is possible to compare values using the ==
operator instead of ʻequal (Object)`.
A class that is guaranteed to have ** 1 instance **.
The following is a description example in Java
Singleton.cs
//Pattern implemented in Enum
//Sealed to become a non-inheritable class
public class SingletonClass
{
//Prevent it from being changed from the outside
private static final SingletonClass INSTANCE = new SingletonClass();
//Prevent it from being generated from the outside
private SingletonClass() { //Initialization process};
//Instance acquisition
public static SingletonClass GetInstance() {return INSTANCE;}
}
The following is a description example in C #
Singleton.cs
//Singleton pattern
public class SingletonClass
{
//Prevent it from being changed from the outside
private static SingletonClass _singleInstance = new SingletonClass();
//Instance acquisition
public static SingletonClass GetInstance(){ return _singleInstance; }
//Prevent it from being generated from the outside
private SingletonClass(){ //Initialization process}
}
Enum A class that is guaranteed to have ** n ** instances. ** * Addition: Java only ** </ font>
Enum.java
enum Season
{
Spring,
Summer,
Autumn,
Winter
}
Addendum: However, C # Enums are not guaranteed to have n instances **. ** ** Unlike Java, which is implemented as a class, C # Enums are just named integers, like C enums, so there is no guarantee similar to Java. See the comments section for details.
It's a snake, but I'm curious, so I'll investigate. In Java, you can implement a singleton using an Enum as follows.
EnumSingleton.java
public enum Singleton {
INSTANCE;
public void execute (String arg) {
//Implement the desired processing such as instance acquisition
}
}
Is it possible to implement a singleton in Enum in C # as well? That is doubtful. ~~ As a result of investigation, it turned out that ** C # cannot define fields and methods in Enum itself, so it is difficult to create a Singleton like Java. ** ** If you implement it using Enum, you need to use ** extension method ** as below ~~ Addendum: As pointed out in the comments, C # Enums are just named integers, so we cannot guarantee the limit on the number of instances. Therefore, ** C # cannot implement singleton using Enum **
--Effective Java 2nd Edition -Study Effective Java [Chapter 2] -[Study session news] Java enumeration -Singleton pattern in C # -Enumeration (C # reference) -sealed (C # reference) -[Enumerated Singleton](https://riptutorial.com/ja/java/example/1870/%E5%88%97%E6%8C%99%E5%9E%8B%E3%82%B7%E3% 83% B3% E3% 82% B0% E3% 83% AB% E3% 83% 88% E3% 83% B3) -When you want to define a field method in C # enum [from Java to C #] -Differences between C # and Java enums
Recommended Posts