๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[JAVA ๋””์ž์ธ ํŒจํ„ด] static์„ ์‘์šฉํ•œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton Pattern) ๊ตฌํ˜„

by Leica 2020. 6. 18.
๋ฐ˜์‘ํ˜•

[JAVA ๋””์ž์ธ ํŒจํ„ด] static์„ ์‘์šฉํ•œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton Pattern) ๊ตฌํ˜„

ํด๋ž˜์Šค ๋‚ด์˜ ๋ณ€์ˆ˜์— static ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ด๋ฉด ๊ทธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ณต์œ ํ•˜๋Š” ํด๋ž˜์Šค ๋ณ€์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ๋ด…์‹œ๋‹ค. ์‚ฌ์šฉ์ž ํด๋ž˜์Šค User๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋งˆ๋‹ค ์ฒซ ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค์—๋Š” 1001์„, ๋‘ ๋ฒˆ์งธ ์ธ์Šคํ„ด์Šค์—๋Š” 1002, ๋‹ค์Œ์€ 1003, ... ์ด๋Ÿฐ์‹์œผ๋กœ 1000๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ id๋ฅผ ๋ถ€์—ฌํ•ด์ฃผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

/*
์‚ฌ์šฉ์ž ํด๋ž˜์Šค
 */
public class User {

    private static Long serialNumber = 1000L;
    private Long userId;
    private String userName;

    public User(String userName) {
        this.userName = userName;
        serialNumber++;
        this.userId = serialNumber;
    }

    public static Long getSerialNumber() {
        return serialNumber;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                '}';
    }

    public static void main(String[] args) {

        System.out.println(User.getSerialNumber());     // serialNumber: 1000

        User user1 = new User("๊น€ํ˜„์ •");
        System.out.println(User.getSerialNumber());     // serialNumber: 1001

        User user2 = new User("์„œ์ง€์€");
        System.out.println(User.getSerialNumber());     // serialNumber: 1002

        User user3 = new User("๋ฐ•์ฃผ์˜");
        System.out.println(User.getSerialNumber());     // serialNumber: 1003

        System.out.println(user1);
        System.out.println(user2);
        System.out.println(user3);
    }
}

 

User ํด๋ž˜์Šค์˜ serialNumber ๋ณ€์ˆ˜์— static ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰ serialNumber๋Š” static ๋ณ€์ˆ˜(ํด๋ž˜์Šค ๋ณ€์ˆ˜)๋กœ์จ, User ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•˜๋Š” ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ณต์œ ํ•˜๋Š” ๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. 1000๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•  ๊ฒƒ์ด๋ฏ€๋กœ 1000์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ์ค‘์š”ํ•˜๊ฒŒ ๋ณผ ๊ฒƒ์€ ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜์ง€์š”.

๊ทธ๋ž˜์„œ ์ƒ์„ฑ์ž ์•ˆ์— serialNumber๋ฅผ 1 ์ฆ๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ User()๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งˆ๋‹ค serialNumber๋Š” 1000๋ถ€ํ„ฐ 1์”ฉ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  serialNumber๋Š” ์™ธ๋ถ€์—์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋„๋ก private ์ ‘๊ทผ์ œ์–ด์ž๋ฅผ ๋ถ™์—ฌ์ฃผ๊ณ  ๊ฐ’์„ ๋ฐ›์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก getter๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ main ๋ฉ”์†Œ๋“œ์—์„œ User ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ๋งŒ๋“ค๋ฉฐ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.

๊ตฌ์ƒํ•œ ๋Œ€๋กœ, ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋งˆ๋‹ค User ํด๋ž˜์Šค์˜ serialNumber๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๊ณ  ๊ฐ ์ธ์Šคํ„ด์Šค์˜ userId ๋ณ€์ˆ˜์— ์ฆ๊ฐ€๋œ serialNumber๊ฐ€ ํ• ๋‹น๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

1000
1001
1002
1003
User{userId=1001, userName='๊น€ํ˜„์ •'}
User{userId=1002, userName='์„œ์ง€์€'}
User{userId=1003, userName='๋ฐ•์ฃผ์˜'}
1003

Process finished with exit code 0

 

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton Pattern)

์ด๋ ‡๊ฒŒ static ๋ณ€์ˆ˜๋Š” ๋‹จ์ˆœํžˆ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๋“ค์ด ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋„ ์‚ฌ์šฉ๋˜์ง€๋งŒ ์ด๋Ÿฌํ•œ static ํ‚ค์›Œ๋“œ์˜ ํŠน์„ฑ์„ ์ด์šฉํ•œ ๋””์ž์ธํŒจํ„ด์ด ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(Singleton Pattern) ์ž…๋‹ˆ๋‹ค. ๋””์ž์ธ ํŒจํ„ด์€ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์„ค๊ณ„ํ•  ๋•Œ ํšจ์œจ์ ์ด๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ์„ค๊ณ„ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์•ˆ์—์„œ ๋‹จ ํ•œ ๊ฐœ๋งŒ ์กด์žฌํ•ด์•ผ ํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€๋ฅผ ์ง€ํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

โ‘  private ์ƒ์„ฑ์ž
โ‘ก static ๋ณ€์ˆ˜๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
โ‘ข ๊ฐ์ฒด์˜ getter ๊ตฌํ˜„

ํด๋ž˜์Šค๋ฅผ ์œ„์™€ ๊ฐ™์ด ์„ค๊ณ„ํ•˜๋ฉด ์ธ์Šคํ„ด์Šค์˜ ์œ ์ผ์„ฑ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.

 

์–ด๋–ค ํ”„๋กœ์ ํŠธ์—์„œ ์ƒ์  ํด๋ž˜์Šค Store์˜ ๊ฐ์ฒด๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋‹จ ํ•œ ๊ฐœ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

Store ํด๋ž˜์Šค๋ฅผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ๊ตฌํ˜„ํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

 

public class Store {

    private static Store instance = new Store();

    private Store() { }

    public static Store getInstance() {
        return instance;
    }
}

public class StoreTest {

    public static void main(String[] args) {

        Store store1 = Store.getInstance();
        Store store2 = Store.getInstance();
        //Store store3 = new Store();   // ์ง์ ‘ ์ƒ์„ฑ ๋ถˆ๊ฐ€

        System.out.println(store1);
        System.out.println(store2);
    }
}

 

Store ํด๋ž˜์Šค ๋‚ด์— Store ํƒ€์ž…์˜ static ๋ณ€์ˆ˜ instance๋ฅผ ์„ ์–ธํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋Š” static ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์— ํ• ๋‹น๋˜์–ด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ์ด ๊ฐ์ฒด๋งŒ์„ ์œ ์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์™œ๋ƒ๋ฉด, ์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ์ œ์–ด์ž๋ฅผ private์œผ๋กœ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— Store ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋Š” ์ ˆ๋Œ€๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก Store ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” static getter๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

StoreTest ํด๋ž˜์Šค์˜ main ๋ฉ”์†Œ๋“œ์—์„œ Store ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

new Store()์™€ ๊ฐ™์ด ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ณ  Store์˜ getInstance()๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ Store ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

com.yoon._static.Store@2c8d66b2
com.yoon._static.Store@2c8d66b2

Process finished with exit code 0

 

store1, store2๋ฅผ ์ฐ์–ด๋ณด๋ฉด ํ•ด์‹œ์ฝ”๋“œ ๊ฐ’์ด ๋™์ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, ๋‘ ๋ณ€์ˆ˜๋Š” ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ž๋ฐ” API์—์„œ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด ์ ์šฉ๋œ ์˜ˆ๋กœ๋Š” Calendar ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

// Calendar calendar = new Calendar(); // ์ง์ ‘ ์ƒ์„ฑ ๋ถˆ๊ฐ€
Calendar calendar = Calendar.getInstance();

Calendar ํด๋ž˜์Šค๋„ ์œ„์—์„œ ๊ตฌํ˜„ํ•œ Store ํด๋ž˜์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด ์ ์šฉ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ Calendar์˜ static ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” getInstance()๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ด์ฒ˜๋Ÿผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ์ง์ ‘ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ๋„, ์ ์šฉ๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ๋„ ๋งŽ์€ ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€