Singleton Pattern
- 전역 변수를 사용하지 않고 object 하나만 생성하여, 어디에서든지 참조할 수 있도록 하는 패턴
- 하나의 instance를 생성하며,
getInstance
method를 통해 모든 client에게 동일한 instance를 반환해주는 작업
advantage of Singleton design pattern
- Object를 하나만 사용하기 때문에 memory를 절약할 수 있다
- 하나의 instance를 지속적으로 재사용 가능하다.
- 전역변수(Global Variable)의 역할을 수행한다.
- Multi-threaded와 database application에서 주로 사용된다.
Example of Singleton Pattern
Example1
더보기
// Main.java
import java.io.*;
public class Main {
public void createSingleton() {
MySingleton ms1 = MySingleton.createMySingleton();
MySingleton ms2 = MySingleton.createMySingleton();
System.out.println( ms1 == ms2 );
}
public static void main(String[] args) {
new Main().createSingleton();
}
}
// MySingleton.java
import java.util.*;
public class MySingleton {
private static MySingleton theObject;
private MySingleton() { }
public static MySingleton createMySingleton() {
if (theObject == null)
theObject = new MySingleton();
return theObject;
}
}
// Output: true
Example2
더보기
// SingleObject.java
public class SingleObject {
private static SingleObject instance = new SingleObject();
private SingleObject(){ }
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
// SingletonPatternDemo.java
public class SingletonPatternDemo {
public static void main(String[] args) {
SingleObject object = SingleObject.getInstance();
object.showMessage();
}
}
// Output: Hello World!
// Another version
// SingletonPatternDemo2.java
public class SingletonPatternDemo2 {
public static void main(String[] args) {
SingleObject object1 = SingleObject.getInstance();
SingleObject object2 = SingleObject.getInstance();
// show the message
object1.showMessage();
object2.showMessage();
System.out.println(object1 == object2) ;
}
}
/*
Output:
Hello World!
Hello World!
true
*/
Example3
더보기
// TicketMaker.java
public class TicketMaker {
private int ticket = 1000;
private static TicketMaker singleton = new TicketMaker();
private TicketMaker() { }
public static TicketMaker getInstance() {
return singleton;
}
public synchronized int getNextTicketNumber() {
return ticket++;
}
}
// TicketMain.java
public class TicketMain {
public static void main(String[] args) {
System.out.println("Start.");
for (int i = 0; i < 10; i++) {
System.out.println(i + ":" + TicketMaker.getInstance().getNextTicketNumber());
}
System.out.println("End.");
}
}
/*
Output:
Start.
0:1000
1:1001
2:1002
3:1003
4:1004
5:1005
6:1006
7:1007
8:1008
9:1009
End.
*/
Example4
더보기
// Triple.java
public class Triple {
private static Triple[] triple = new Triple[]{
new Triple(0),
new Triple(1),
new Triple(2),
};
private int id;
private Triple(int id) {
System.out.println("The instance " + id + " is created.");
this.id = id;
}
public static Triple getInstance(int id) {
return triple[id];
}
public String toString() {
return "[Triple id=" + id + "]";
}
}
// TripleMain.java
public class TripleMain {
public static void main(String[] args) {
System.out.println("Start.");
for (int i = 0; i < 9; i++) {
Triple triple = Triple.getInstance(i % 3);
System.out.println(i + ":" + triple);
}
System.out.println("End.");
}
}
/*
Output:
Start.
The instance 0 is created.
The instance 1 is created.
The instance 2 is created.
0:[Triple id=0]
1:[Triple id=1]
2:[Triple id=2]
3:[Triple id=0]
4:[Triple id=1]
5:[Triple id=2]
6:[Triple id=0]
7:[Triple id=1]
8:[Triple id=2]
End.
*/