๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Java] ์Šคํ…Œํ‹ฑ ๋ฉ”์†Œ๋“œ์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

์ถœ์ฒ˜

ChatGPT


1. ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด Singleton Pattern

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

 

์˜ˆ์ œ ์ฝ”๋“œ

public class Singleton {
    // ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ •์  ๋ณ€์ˆ˜๋กœ ์ €์žฅ
    private static Singleton instance;

    // private ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ถˆ๊ฐ€
    private Singleton() {
        // ์ดˆ๊ธฐํ™” ์ž‘์—… ์ˆ˜ํ–‰
    }

    // ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •์  ๋ฉ”์„œ๋“œ
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
    public void showMessage() {
        System.out.println("Hello from Singleton instance!");
    }
}

public class Main {
    public static void main(String[] args) {
        // ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ
        Singleton.getInstance().showMessage();
    }
}

 

์„ค๋ช…

 

Singleton ํด๋ž˜์Šค

  • instance๋ผ๋Š” ์ •์  ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • getInstance() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • showMessage()๋Š” ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ, ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

Main ํด๋ž˜์Šค

  • Singleton.getInstance().showMessage();๋ฅผ ํ†ตํ•ด ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ด๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

2. ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ Static Factory Method

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์ ์œผ๋กœ ์ œ๊ณตํ•ด ๊ฐ์ฒด ์ƒ์„ฑ ๊ณผ์ •์„ ์บก์Šํ™”ํ•œ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

public class MyClass {
    // ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ
    public static MyClass createInstance() {
        return new MyClass();
    }

    // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ
    public void displayMessage() {
        System.out.println("Instance method called!");
    }
}

public class Main {
    public static void main(String[] args) {
        // ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ
        MyClass myObject = MyClass.createInstance();

        // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        myObject.displayMessage();
    }
}

 

์„ค๋ช…

 

  • MyClass ํด๋ž˜์Šค:
    • createInstance()๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋กœ, MyClass์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • displayMessage()๋Š” ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ, ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  • Main ํด๋ž˜์Šค:
    • MyClass.createInstance()๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , displayMessage() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

3. ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ต๋ช… ํด๋ž˜์Šค ํ™œ์šฉ

์ž๋ฐ”์—์„œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ์ต๋ช… ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ํŠน์ • ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

interface Action {
    void execute();
}

public class Main {
    public static void main(String[] args) {
        // ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด๋ฅผ ์ต๋ช… ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ์ƒ์„ฑ
        Action action = new Action() {
            @Override
            public void execute() {
                System.out.println("Executing action!");
            }
        };

        // ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ์—์„œ ์ต๋ช… ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        performAction(action);
    }

    // ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ
    public static void performAction(Action action) {
        action.execute(); // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    }
}

 

 

 

์„ค๋ช…

 

Action ์ธํ„ฐํŽ˜์ด์Šค:

  • execute() ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•œ๋‹ค.

Main ํด๋ž˜์Šค:

  • performAction()์€ ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ๋กœ, ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์„ ์ธ์ž๋กœ ๋ฐ›์•„ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  • new Action() { ... }๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ต๋ช… ํด๋ž˜์Šค ํ˜•ํƒœ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

4. ์ž๋ฐ” 8 ์ดํ›„์˜ ๋žŒ๋‹ค ํ‘œํ˜„์‹

์ž๋ฐ” 8์—์„œ๋Š” ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ํ†ตํ•ด ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
        // ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ Consumer ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
        Consumer<String> consumer = message -> System.out.println(message);

        // ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ์—์„œ ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ํ†ตํ•œ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        executeConsumer(consumer, "Hello with Lambda!");
    }

    // ์Šคํƒœํ‹ฑ ๋ฉ”์„œ๋“œ
    public static void executeConsumer(Consumer<String> consumer, String message) {
        consumer.accept(message); // ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    }
}

 

์„ค๋ช…

 

Consumer ์ธํ„ฐํŽ˜์ด์Šค:

  • accept(T t) ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ์†Œ๋น„ํ•˜๋Š” ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.

Main ํด๋ž˜์Šค:

  • ๋žŒ๋‹ค ํ‘œํ˜„์‹ message -> System.out.println(message)๋ฅผ ํ†ตํ•ด Consumer<String> ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
  • executeConsumer() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋žŒ๋‹ค ํ‘œํ˜„์‹์„ ์‹คํ–‰ํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

5. ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ธ์Šคํ„ด์Šค ์‚ฌ์šฉ

ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

public class Printer {
    private static final Printer instance = new Printer();

    private Printer() {
        // ์ƒ์„ฑ์ž ์ˆจ๊น€
    }

    public static Printer getInstance() {
        return instance;
    }

    public void print(String message) {
        System.out.println(message);
    }
}

public class Main {
    public static void main(String[] args) {
        // ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
        Printer.getInstance().print("Printing using pre-defined instance!");
    }
}

 

 

 

์„ค๋ช…

 

Printer ํด๋ž˜์Šค:

  • instance๋ผ๋Š” ์ •์  ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค์˜ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • getInstance() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฏธ๋ฆฌ ์ •์˜๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , print() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

Main ํด๋ž˜์Šค:

  • Printer.getInstance().print(...);๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.