[JAVA] Design pattern ~ Singleton ~

1.First of all

Here is a summary of the ** Singleton pattern ** in the GoF design pattern.

2. What is the Singleton pattern?

--Singleton means ** a set with only one element **. --The Singleton pattern is a ** method that guarantees that there is only one instance **. --For example, a class that expresses the system settings, a class that expresses the window system, and so on. --In the GoF design pattern, it is classified as ** Design pattern for generation **.

3. Sample class diagram

Singleton.PNG

4. Sample program

A program that creates a singleton instance.

4-1. Singleton class

A class that returns only one instance. The constructor for the Singleton class is private. This is to prohibit calling the constructor from outside the Singleton class.

Singleton.cs


public class Singleton {

	private static Singleton singleton = new Singleton();

	private Singleton() {
		System.out.println("You have created an instance.");
	}

	public static Singleton getInstance() {
		return singleton;
	}
}

4-2. Main class

This class performs the main processing.

Main.cs


public class Main {
	public static void main(String[] args) {
		Singleton obj1 = Singleton.getInstance();
		Singleton obj2 = Singleton.getInstance();
		if (obj1 == obj2) {
			System.out.println("obj1 and obj2 are the same instance.");
		} else {
			System.out.println("obj1 and obj2 are not the same instance.");
		}
	}
}

4-3. Execution result

You have created an instance.
obj1 and obj2 are the same instance.

5. Benefits

The Singleton pattern puts a limit on the number of instances. If you have multiple instances, the instances can interact with each other and create unexpected bugs. However, if you guarantee that you have only one instance, you can program with that prerequisite.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

This article and sample program were created based on the following books.

-** Introduction to design patterns learned in Java language **

It was very easy to understand and I learned a lot. Thank you. The detailed explanations of design patterns and sample programs are written, so please take a look at the books as well.

Recommended Posts

Design pattern ~ Singleton ~
Singleton Pattern
Singleton pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern