티스토리 뷰

728x90
창조 패턴(Creational Patterns)은 객체 생성 방식을 체계적으로 관리하고, 객체 생성의 세부 사항을 숨겨 시스템의 유연성과 확장성을 높이는 데 초점을 맞춘 디자인 패턴입니다. 아래에서는 주요 창조 패턴인 Singleton, Factory Method, Abstract Factory, Builder, Prototype 패턴에 대해 설명하고, 각각 자바 예제를 통해 이해를 돕겠습니다.

1. Singleton 패턴
  • 설명: 클래스의 인스턴스가 시스템 전체에서 단 하나만 존재하도록 보장하며, 전역적으로 접근할 수 있도록 합니다. 로그 관리 시스템, 설정 관리자 등 단일 인스턴스를 공유해야 하는 상황에서 유용합니다.
  • 특징:
    • 생성자를 private으로 제한하여 외부에서 직접 객체를 생성하지 못하게 합니다.
    • 정적 메서드를 통해 단일 인스턴스에 접근합니다.
  • 자바 예제:
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("Singleton 인스턴스에서 메시지 출력");
    }
}

// 사용 예
public class Main {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        singleton.showMessage();  // "Singleton 인스턴스에서 메시지 출력" 출력
    }
}

2. Factory Method 패턴
  • 설명: 객체 생성을 서브클래스에 위임하여, 런타임에 생성할 객체의 유형을 결정할 수 있도록 합니다. 객체 생성 로직을 분리하여 유연성을 높이는 데 유용하며, 다양한 유형의 문서 생성과 같은 상황에서 사용됩니다.
  • 특징:
    • 추상 메서드 또는 인터페이스를 통해 객체 생성 방식을 정의합니다.
    • 서브클래스에서 구체적인 객체 생성 로직을 구현합니다.
  • 자바 예제:
// 문서 인터페이스
interface Document {
    void open();
}

// PDF 문서 클래스
class PDFDocument implements Document {
    public void open() {
        System.out.println("PDF 문서 열기");
    }
}

// Word 문서 클래스
class WordDocument implements Document {
    public void open() {
        System.out.println("Word 문서 열기");
    }
}

// 문서 팩토리 추상 클래스
abstract class DocumentFactory {
    public abstract Document createDocument();
}

// PDF 문서 팩토리
class PDFDocumentFactory extends DocumentFactory {
    public Document createDocument() {
        return new PDFDocument();
    }
}

// Word 문서 팩토리
class WordDocumentFactory extends DocumentFactory {
    public Document createDocument() {
        return new WordDocument();
    }
}

// 사용 예
public class Main {
    public static void main(String[] args) {
        // PDF 문서 생성
        DocumentFactory factory = new PDFDocumentFactory();
        Document doc = factory.createDocument();
        doc.open();  // "PDF 문서 열기" 출력
    }
}

3. Abstract Factory 패턴
  • 설명: 관련된 객체 가족을 생성할 수 있는 인터페이스를 제공하며, 구체 클래스를 직접 지정하지 않습니다. 서로 다른 플랫폼이나 환경에 맞는 객체를 생성할 때 유용하며, GUI 툴킷, 플랫폼별 위젯 생성 등에 사용됩니다.
  • 특징:
    • 관련된 객체들을 그룹화하여 일관성 있는 생성을 보장합니다.
    • 플랫폼 독립적인 코드를 작성할 수 있습니다.
  • 자바 예제:
// 버튼 인터페이스
interface Button {
    void render();
}

// Windows 버튼
class WindowsButton implements Button {
    public void render() {
        System.out.println("Windows 버튼 렌더링");
    }
}

// Mac 버튼
class MacButton implements Button {
    public void render() {
        System.out.println("Mac 버튼 렌더링");
    }
}

// GUI 팩토리 인터페이스
interface GUIFactory {
    Button createButton();
}

// Windows 팩토리
class WindowsFactory implements GUIFactory {
    public Button createButton() {
        return new WindowsButton();
    }
}

// Mac 팩토리
class MacFactory implements GUIFactory {
    public Button createButton() {
        return new MacButton();
    }
}

// 사용 예
public class Main {
    public static void main(String[] args) {
        GUIFactory factory = new WindowsFactory();
        Button button = factory.createButton();
        button.render();  // "Windows 버튼 렌더링" 출력
    }
}

4. Builder 패턴
  • 설명: 복잡한 객체를 단계적으로 구성하며, 객체의 표현과 생성 과정을 분리합니다. 다양한 구성으로 객체를 생성할 때 유용하며, 자동차 옵션 설정 등에 사용됩니다.
  • 특징:
    • 빌더 클래스를 통해 객체를 단계적으로 구성합니다.
    • 최종 객체는 빌더의 정보를 기반으로 생성됩니다.
  • 자바 예제:
// 자동차 클래스
class Car {
    private String engine;
    private String color;
    private String wheels;

    // private 생성자, 빌더를 통해서만 객체 생성 가능
    private Car(Builder builder) {
        this.engine = builder.engine;
        this.color = builder.color;
        this.wheels = builder.wheels;
    }

    // 내부 빌더 클래스
    public static class Builder {
        private String engine;
        private String color;
        private String wheels;

        public Builder setEngine(String engine) {
            this.engine = engine;
            return this;
        }

        public Builder setColor(String color) {
            this.color = color;
            return this;
        }

        public Builder setWheels(String wheels) {
            this.wheels = wheels;
            return this;
        }

        public Car build() {
            return new Car(this);
        }
    }

    @Override
    public String toString() {
        return "Car [engine=" + engine + ", color=" + color + ", wheels=" + wheels + "]";
    }
}

// 사용 예
public class Main {
    public static void main(String[] args) {
        Car car = new Car.Builder()
                .setEngine("V8")
                .setColor("Red")
                .setWheels("Alloy")
                .build();
        System.out.println(car);  // "Car [engine=V8, color=Red, wheels=Alloy]" 출력
    }
}

5. Prototype 패턴
  • 설명: 기존 객체를 복사(클론)하여 새로운 객체를 생성합니다. 객체 생성 비용이 높거나 복잡한 경우 효율적이며, 그래픽 객체와 같은 리소스 절약을 위해 사용됩니다.
  • 특징:
    • 객체 복제를 통해 생성 시간을 단축합니다.
    • 복제된 객체는 기존 객체와 독립적으로 동작합니다.
  • 자바 예제:
import java.util.HashMap;
import java.util.Map;

// 프로토타입 인터페이스
interface Prototype {
    Prototype clone();
}

// 구체적인 프로토타입 클래스
class ConcretePrototype implements Prototype {
    private String field;

    public ConcretePrototype(String field) {
        this.field = field;
    }

    @Override
    public Prototype clone() {
        return new ConcretePrototype(this.field);
    }

    @Override
    public String toString() {
        return "ConcretePrototype [field=" + field + "]";
    }
}

// 프로토타입 레지스트리
class PrototypeRegistry {
    private Map<String, Prototype> prototypes = new HashMap<>();

    public void addPrototype(String key, Prototype prototype) {
        prototypes.put(key, prototype);
    }

    public Prototype getPrototype(String key) {
        return prototypes.get(key).clone();
    }
}

// 사용 예
public class Main {
    public static void main(String[] args) {
        PrototypeRegistry registry = new PrototypeRegistry();
        ConcretePrototype original = new ConcretePrototype("Original");
        registry.addPrototype("key", original);

        Prototype cloned = registry.getPrototype("key");
        System.out.println(cloned);  // "ConcretePrototype [field=Original]" 출력
    }
}

요약
  • Singleton: 단일 인스턴스 보장, 전역 접근 제공.
  • Factory Method: 객체 생성을 서브클래스에 위임, 유연한 객체 생성.
  • Abstract Factory: 관련 객체 가족을 생성, 플랫폼 독립성 제공.
  • Builder: 복잡한 객체를 단계적으로 구성, 다양한 구성 지원.
  • Prototype: 기존 객체를 복사하여 효율적으로 객체 생성.
이 패턴들은 객체 생성 과정을 추상화하여 시스템의 유연성과 확장성을 높이며, 각기 다른 상황에서 적합한 객체 생성 방식을 제공합니다.
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함