[JAVA] What is instance control?

Introduction

<!-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).

table of contents

<!-Edit title and anchor name->

  1. What is the state after all?
  2. What is a concrete example?
  3. Can I make a singleton with Enum in C #?
  4. References (#reference)

<!-Each chapter->

What is the state after all?

--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)`.

What is a specific example?

Singleton

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.

Can I make a singleton with Enum in C #?

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 **

References

--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

What is instance control?
What is @ (instance variable)?
[Ruby] What is an instance?
What is Cubby
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is docker run -it?
What is Java Assertion? Summary.