LoginSignup
3
3

More than 5 years have passed since last update.

デザインパターン #5 〜Singleton〜

Posted at

第5回目 Singletonパターン

概要

Singletonパターンはそのインスタンスが1つしか存在しないことを保証するためデザインパターンです。

サンプル

SingletonObject.java
public class SingletonObject {
    private static SingletonObject object = new SingletonObject();
    private SingletonObject() {
    }
    public static SingletonObject getInstance(){
        return object;
    }
}
Main.java
public class Main {
    public static void main(String[] args){
        SingletonObject object1 = SingletonObject.getInstance();
        SingletonObject object2 = SingletonObject.getInstance();
        System.out.print(object1.equals(object2))
    }
}

結果

true
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3