ํด๋์ค์ ์ธ์คํด์ค๊ฐ ๋ฑ 1๊ฐ๋ง ์์ฑ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ ๋์์ธ ํจํด์ด๋ค.
๊ฐ์ฒด๋ฅผ new ํค์๋๋ฅผ ํตํด ์ง์ ์์ฑํ์ง ์๊ณ getInstance() ๋ฉ์๋์ ๊ฐ์ด ์ ์๋ ๋ฐฉ์์ผ๋ก๋ง ๊ฐ์ ธ์จ๋ค.
์ฑ๊ธํค ํจํด์ ํ๋์ ์ธ์คํด์ค๋ฅผ ๊ณต์ ํ๊ธฐ ๋๋ฌธ์ ๋ถํ์ํ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ฅผ ์ค์ผ ์ ์๊ณ ๋ฐ๋ณต์ ์ธ ๊ฐ์ฒด ์์ฑ์ ํผํ ์ ์์ด ์ฑ๋ฅ๋ฉด์์๋ ์ด์ ์ ๊ฐ์ง ์ ์๋ค.
public class Singleton {
//์ ์ ์ฐธ์กฐ๋ณ์
private static Singleton singleton;
//new ํค์๋๋ฅผ ๋ง๊ธฐ์ํด ์์ฑ์์ private ์ ๊ทผ์ ์ด์๋ฅผ ์ง์
private Singleton(){
}
//getInstance() ๋ฉ์๋ ์ฒซ ํธ์ถ์ ๊ฐ์ฒด ์์ฑ, ์ดํ ํธ์ถ์ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฌ์ด
public static Singleton getInstance(){
if(singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
์ฑ๊ธํค ํจํด์ ์ ์ฉํ ์ ๋ ๊ฐ ์ด์์ ๊ฐ์ฒด๋ ์กด์ฌํ ์ ์๋ค.
์ด๋ฅผ ๊ตฌํํ๊ธฐ ์ํด ๊ฐ์ฒด ์์ฑ์ ์ํ ์์ฑ์์ ์ ์ฝ์ ๊ฑธ๊ณ ๋จ์ผ ๊ฐ์ฒด๋ฅผ
๋ฐํํ ์ ์๋๋ก ๋ฉ์๋๋ฅผ ํ์ฉํด์ผ ํ๋ค.
public class SingletonExam {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
Singleton s3 = Singleton.getInstance();
System.out.println(s1); //singleton.Singleton@3b07d329
System.out.println(s2); //singleton.Singleton@3b07d329
System.out.println(s3); //singleton.Singleton@3b07d329
System.out.println(s1 == s2); //true
System.out.println(s2 == s3); //true
}
}
getInstance() ๋ฉ์๋๋ฅผ ํตํด ํธ์ถ๋ s1, s2, s3 ์ฐธ์กฐ๋ณ์๊ฐ ๋ชจ๋ ๊ฐ์ ์ฐธ์กฐ๊ฐ์ ๊ฐ์ง๊ณ ์๋ค.
์ด๋ new ํค์๋๋ฅผ ํตํ ๊ฐ์ฒด ์์ฑ์ด ์๋ ์ด๋ฏธ ์กด์ฌํ๋ ์ ์ผํ ๋จ์ผ ๊ฐ์ฒด๋ฅผ ๋ฆฌํด๋ฐ๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์ฑ๊ธํค ํจํด์ ๋ฏธ๋ฆฌ ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ ์ ๋ฉ์๋๋ฅผ ํตํด ์ฌ์ฉํ๊ธฐ์ ํด๋์ค ๊ฐ ์์กด์ฑ์ด ๋์์ง๋ค.
์ฑ๊ธํค ์ธ์คํด์ค์ ๋ณ๊ฒฝ ์ ํด๋น ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๋ ๋ชจ๋ ํด๋์ค๋ฅผ ์์ ํด์ผ ํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ฑ๊ธํค ํจํด์ ๊ธฐ๋ณธ์ ์ผ๋ก private ์์ฑ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ์ค๋ณต ์์ฑ์ ๋ฐฉ์งํ๋ค.
์ด๋ก ์ธํด ์์์ ํตํ ์์ํด๋์ค๋ฅผ ๋ง๋ค์ ์๊ฒ ๋๋ฉฐ ๊ฐ์ฒด์งํฅ์ ์ฅ์ ์ค ํ๋์ธ ๋คํ์ฑ์ ํ์ฉํ๊ธฐ ์ด๋ ต๋ค.
์ฑ๊ธํค ๊ฐ์ฒด๋ ์ ์ญ์ ์ผ๋ก ํ๋๋ง ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๋
๋ฆฝ์ ์ธ ํ
์คํธ ํ๊ฒฝ์ ๋ง๋ค๊ธฐ ์ด๋ ต๋ค.
๋จ์ ํ
์คํธ์์ ์ฑ๊ธํค ์ธ์คํด์ค๋ฅผ ๋ชจํนํ๊ฑฐ๋ ๊ต์ฒดํ๊ธฐ๊ฐ ์ด๋ ค์ ํ
์คํธ์ ์ ์ฐ์ฑ์ด ๋จ์ด์ง๊ณ ์ ์์ ์ผ๋ก ์ํ๋์ง ์์ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
๋ชจํน(Mocking): ๋จ์ ํ ์คํธ์์ ํน์ ๊ฐ์ฒด๋ ํด๋์ค๋ฅผ ์ค์ ๊ตฌํ์ด ์๋ ๊ฐ์ง ๊ฐ์ฒด(Mock ๊ฐ์ฒด) ๋ก ๋์ฒดํ์ฌ ํ ์คํธํ๋ ๊ธฐ๋ฒ
์ด๋ฌํ ๋ฌธ์ ์ ๋ค ๋๋ฌธ์ ์ฑ๊ธํคํจํด์ ์ํฐํจํด์ด๋ผ ๋ถ๋ฆฌ๊ธฐ๋ ํ๋ค.
์ฌ๋ฌ ์ค๋ ๋๊ฐ ๋์์ ์ ๊ทผํ๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋ ๋ฌธ์
public class SingletonThread{
public static void main(String[] args) {
Thread t1 = new Thread(()->{
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
});
Thread t2 = new Thread(()->{
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
});
Thread t3 = new Thread(()->{
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
});
t1.start(); //singleton.Singleton@530def55
t2.start(); //singleton.Singleton@7ddc7898
t3.start(); //singleton.Singleton@7ddc7898
}
}
์ฌ๋ฌ ์ฐ๋ ๋๊ฐ ๋์์ ์คํ๋๋ฉฐ instance == null ์กฐ๊ฑด์ ๋ง์กฑํ๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๊ณ ์๋ํ๋ค.
์ด๋ก์ธํด ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ์์ฑ๋์ด t1, t2 ์ฐ๋ ๋๊ฐ ๋ค๋ฅธ ์ฐธ์กฐ๊ฐ์ ๊ฐ์ง๊ฒ ๋๋ฉฐ ์ฑ๊ธํค ํจํด์ด ๊นจ์ง๊ณ ๋ง์๋ค.
๋์์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉฐ Thread-safe ํ์ง ๋ชปํ ์ํฉ์ด ์ผ์ด๋ฌ๋ค.
Thread-safe:
๋ฉํฐ์ค๋ ๋ ํ๊ฒฝ์์ ์ฌ๋ฌ ์ค๋ ๋๊ฐ ๋์์ ์ ๊ทผํด๋ ํ๋ก๊ทธ๋จ์ด ์ ์์ ์ผ๋ก ๋์ํ๋ ํน์ฑ
๋๊ธฐํ(syncronuized)//getInstance() ๋ฉ์๋์ syncronized ํค์๋ ์ถ๊ฐ
public static synchronized Singleton getInstance(){
if(singleton == null) {
singleton = new Singleton();
}
return singleton;
}
์ด๋ฅธ ์ด๊ธฐํ(Eager Initialization)final ํค์๋๋ฅผ ํตํด ๋ถ๋ณ ๊ฐ์ฒด๋ก ๋ง๋ค์ด์ง๋ค.public class Singleton {
//๋ณ์ ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
private static final Singleton singleton = new Singleton();
//new ํค์๋๋ฅผ ๋ง๊ธฐ์ํด ์์ฑ์์ private ์ ๊ทผ์ ์ด์๋ฅผ ์ง์
private Singleton(){
}
//getInstance() ๋ฉ์๋
public static Singleton getInstance(){
return singleton;
}
}
๋๋ธ ์ฒดํฌ ๊ธฐ๋ฒ(Double-Checked Locking)public class Singleton {
//volatile ํค์๋ ์ฌ์ฉ
private static volatile Singleton singleton ;
private Singleton(){
}
//getInstance() ๋ฉ์๋
public static Singleton getInstance(){
if(singleton == null) { //1์ฐจ๊ฒ์ฌ
synchronized (Singleton.class) {
if (singleton == null) { //2์ฐจ๊ฒ์ฌ
singleton = new Singleton();
}
}
}
return singleton;
}
}
volatile ํค์๋๋ฅผ ํตํด ๋ช
๋ น์ด ์ฌ๋ฐฐ์น๋ฅผ ๋ง์์ผ ํ๋ค.์ฑ๊ธํค ๊ฐ์ฒด์ ์์ฑ๊ณผ์
- ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐํ๋ณด
- ์์ฑ์ ํธ์ถ
- ์ธ์คํด์ค ๋ณ์์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์ ํ ๋น
์์ฒ๋ผ 3๋จ๊ณ๋ฅผ ๊ฑฐ์น์ง๋ง ๋ช ๋ น์ด ์คํ์์๊ฐ ๊ผฌ์ผ์ ๋ฌธ์ ๊ฐ ์๊ธธ์ ์๋ค. ์ฌ๋ฐฐ์น๊ฐ ๋ฐ์ํ๋ฉด ๊ฐ์ฒด ์์ฑ์ด ๋๋๊ธฐ ์ ์ ์ฐธ์กฐ๊ฐ์ด ๋จผ์ ๋ฐํ๋ ์ ์์ผ๋ฉฐ ํด๋น ์ํฉ ๋ฐ์์ ๋ค๋ฅธ ์ค๋ ๋๊ฐ ์์ฑ๋์ง ์์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ํ์ด ์๊ธด๋ค.
๋ช ๋ น์ด ์ฌ๋ฐฐ์น: ํ๋ก๊ทธ๋จ์ ๋น ๋ฅธ ์คํ์ ์ํด ์์๋ฅผ ์ปดํ์ผ๋ฌ๋ CPU๊ฐ ์์๋ก ๋ณ๊ฒฝํ๋ ๊ฒ