CS/๋””์ž์ธ ํŒจํ„ด

[๋””์ž์ธ ํŒจํ„ด] ์‹ฑ๊ธ€ํ†ค(Singleton) ํŒจํ„ด

๋ฒผ๋ฆฌ01 2024. 1. 10. 21:30

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์€ ์ƒ์„ฑ ํŒจํ„ด ์ค‘ ํ•˜๋‚˜๋กœ, ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ํŠน์ • ์ธ์Šคํ„ด์Šค๊ฐ€ ๋‹จ ํ•˜๋‚˜์ž„์„ ๋ณด์žฅํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•œ๋‹ค.

 

public class Singleton {
    private static Singleton instance;

    // Private constructor to prevent direct instantiation
    private Singleton() {
        // Initialization if needed
    }

    // Static method to provide access to the instance
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    // Other methods or properties can be added here
    public void someMethod() {
        System.out.println("Singleton method called.");
    }
}

 

public class Main {
    public static void main(String[] args) {
        // Accessing the Singleton instance
        Singleton singletonInstance1 = Singleton.getInstance();
        Singleton singletonInstance2 = Singleton.getInstance();

        // Both instances will be the same object
        System.out.println(singletonInstance1 == singletonInstance2); // Output: true

        // Using the Singleton's methods
        singletonInstance1.someMethod(); // Output: Singleton method called.
        singletonInstance2.someMethod(); // Output: Singleton method called.
    }
}

 

์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ ์ œํ•œ์„ `private`์œผ๋กœ ๋‘์–ด ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰๋Š”๋‹ค. `getInstance()` ๋ฉ”์„œ๋“œ์—์„œ ์ž๊ธฐ ์ž์‹  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ  ๋ฉค๋ฒ„ ํ•„๋“œ์— ํ• ๋‹นํ•œ ๋’ค `private` `static`์œผ๋กœ ์„ ์–ธ๋œ ๋ฉค๋ฒ„ ํ•„๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

์ด๋•Œ `instance`๋ผ๋Š” ๋ฉค๋ฒ„ ํ•„๋“œ๋Š” `private`์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๊ณณ์—์„œ ํ•ด๋‹น ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ง์ ‘ ์ฐธ์กฐ๋  ์ผ๋„ ์—†์œผ๋ฏ€๋กœ ๊ตณ์ด `static`์œผ๋กœ ์„ ์–ธ๋˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์•„ ๋ณด์ธ๋‹ค! 

 

์™œ static์ด์–ด์•ผ ํ• ๊นŒ?

์ƒ์„ฑ์ž์˜ ์ ‘๊ทผ ์ œํ•œ์ด `private`์œผ๋กœ ์ œํ•œ ๋˜์–ด์žˆ๋Š” ์ƒํ™ฉ์—์„œ, ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์–ป์œผ๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ `getInstance()` ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค. ์ด๋•Œ `getInstance()`๊ฐ€ `static`์œผ๋กœ ์„ ์–ธ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ๋Š” non-static ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

`static` ์„ฑ์งˆ์„ ๊ฐ€์ง„ ๋ฉค๋ฒ„๋Š” ํด๋ž˜์Šค์— ์ข…์†๋˜๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ ์ฆ‰์‹œ ํด๋ž˜์Šค ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค ๋“ฑ์˜ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ JVM ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์˜ Static area(Method area)์— ์˜ฌ๋ผ๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ ํด๋ž˜์Šค ์ด๋ฆ„์„ ํ†ตํ•ด ์ƒ์‹œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ํ•ด๋‹น ๊ฐ’์„ ๊ณต์œ ํ•œ๋‹ค. ๋ฐ˜๋ฉด ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋Š” ๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ Heap ์˜์—ญ์— ์˜ฌ๋ผ๊ฐ€๊ณ  ๋‚˜์„œ์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ `static` ์œผ๋กœ ์„ ์–ธ๋œ ๋ฉ”์„œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๊ทธ ์‹œ์ ์— ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ํ•ด๋‹น ํ•„๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ•„๋“œ๋กœ ๊ฐ„์ฃผ๋˜๋ฏ€๋กœ `static` ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ๋ฉค๋ฒ„๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.