0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Singleton Pattern

Last updated at Posted at 2018-05-04

Singleton Pattern

Singleton Pattern is a software design pattern that restricts the instance of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instance to a certain number of objects.

overview

The singleton design pattern solves these problems

  • How can it be ensured that a class has only one instance?
  • How can the sole instance of a class be accessed easily?
  • How can a class control its instantiation?
  • How can the number of instances of a class be restricted?

implementation

Singleton.java
public final class Singleton {
    // #1 private instance
    private static final Singleton INSTANCE = new Singleton();

    // #2 private constructor
    private Singleton() {}

    // #3 public method to get a Singleton instance
    public static Singleton getInstance() {
        return INSTANCE;
    }
}

Reference

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?