GoF란?
- Gang of Four (4인방)의 줄임말
- 1994년 "Design Patterns: Elements of Reusable Object-Oriented Software" 책을 쓴 4명의 저자들
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
왜 중요한가요?
- 소프트웨어 개발에서 표준 용어집 역할
- 개발자들 간의 공통 언어
- 검증된 설계 솔루션들
이 책에서 정의한 23개의 디자인 패턴:
1. 생성 패턴 (Creational Patterns) - 5개
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Singleton
2. 구조 패턴 (Structural Patterns) - 7개
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
3. 행위 패턴 (Behavioral Patterns) - 11개
- Observer
- Strategy
- Command
- State
- Template Method
- Visitor
- Chain of Responsibility
- Iterator
- Mediator
- Memento
- Interpreter
Factory 패턴
- 개념: 객체 생성을 전담하는 클래스나 메서드를 만들어서, 클라이언트가 구체적인 클래스를 알 필요 없이 객체를 생성할 수 있게 하는 패턴
- 목적: 객체 생성을 담당
- 범위: 단일 스레드에서 객체 만드는 방법
- Factory: 게임 캐릭터 생성, UI 컴포넌트 생성, API 클라이언트 생성
// 1. 공통 인터페이스
interface DatabaseConnection {
String getConnectionInfo();
}
// 2. 구체적인 구현체들
class MySQLConnection implements DatabaseConnection {
public MySQLConnection(String host, String database) { /* 초기화 */ }
@Override
public String getConnectionInfo() {
return "MySQL 연결 정보";
}
}
class PostgreSQLConnection implements DatabaseConnection {
public PostgreSQLConnection(String host, String database) { /* 초기화 */ }
@Override
public String getConnectionInfo() {
return "PostgreSQL 연결 정보";
}
}
// 3. Factory 클래스 (핵심!)
class DatabaseConnectionFactory {
public static DatabaseConnection createConnection(String dbType, String host, String database) {
switch (dbType.toLowerCase()) {
case "mysql":
return new MySQLConnection(host, database);
case "postgresql":
return new PostgreSQLConnection(host, database);
default:
throw new IllegalArgumentException("지원하지 않는 DB 타입");
}
}
}
// 4. 사용
DatabaseConnection conn = DatabaseConnectionFactory.createConnection("mysql", "localhost", "mydb");
'개발기술 > 설계|소프트웨어패턴' 카테고리의 다른 글
| Saga 패턴 (0) | 2025.12.16 |
|---|---|
| 동시성 고려 프로그래밍(stateless, Thread-Safe, DB Lock) (0) | 2025.11.10 |
| 소프트웨어 패턴 개념과 분류 (0) | 2025.09.22 |
| Consumer-Producer 패턴 (2) | 2025.08.11 |
| 코드패턴 (0) | 2025.03.17 |