브리지 패턴 (Bridge Pattern)
Gof Design Pattern2014. 1. 28. 05:11
Bridge Pattern - 브리지 패턴
- 구현부에서 추상층을 분리하여 각자 독립적으로 변형할 수 있게 하는 패턴이다.
- 즉, 기능과 구현을 별도의 클래스로 정의해서 서로를 분리하는 방법이다
- 전형적인 상속을 이용한 패턴으로 확장 설계에 용이하다
샘플코드)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//------------------------------------------------------------------
// Implementor 인터페이스
class Implementor
{
public:
virtual void operationImpl() = 0;
};
//------------------------------------------------------------------
// ConcreteImplementorA 상속 클래스
class ConcreteImplementorA : public Implementor
{
public:
void operationImpl() { cout << "ConcreteImplementorA" << endl; }
};
//------------------------------------------------------------------
// ConcreteImplementorB 상속 클래스
class ConcreteImplementorB : public Implementor
{
public:
void operationImpl() { cout << "ConcreteImplementorB" << endl; }
};
//------------------------------------------------------------------
// Abstraction 인터페이스
class Abstraction
{
public:
virtual void operation() = 0;
};
//------------------------------------------------------------------
// ConcreteAbstraction 상속 클래스
class ConcreteAbstraction : public Abstraction
{
public:
ConcreteAbstraction(Implementor* i) : impl(i) {}
public:
void operation() { impl->operationImpl(); }
private:
Implementor* impl;
};
//------------------------------------------------------------------
// Main
int _tmain(int argc, _TCHAR* argv[])
{
ConcreteImplementorA pConcreteImplementorA;
ConcreteImplementorB pConcreteImplementorB;
ConcreteAbstraction mConcreteAbstractionA(&pConcreteImplementorA);
ConcreteAbstraction mConcreteAbstractionB(&pConcreteImplementorB);
mConcreteAbstractionA.operation();
mConcreteAbstractionB.operation();
return 0;
}
|
예제를 통한 브리지 패턴(Bridge Pattern) 알아보기
사람이라는 추상화 객체를 각각 요리하는 사람과 청소하는 사람으로 나누기 위해 각각의 구현을 작업이라는 인터페이스를 통해 별도로 분리하였다.
예제 코드)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/** "Implementor" */
class 작업
{
public:
virtual void 처리() = 0;
};
/** "ConcreteImplementor" 1/2 */
class 요리 : public 작업
{
public:
void 처리() override
{
std::cout << "요리를 합니다" << std::endl;
}
};
/** "ConcreteImplementor" 2/2 */
class 청소 : public 작업
{
public:
void 처리() override
{
std::cout << "청소를 합니다" << std::endl;
}
};
/** "Abstraction" */
class 사람
{
public:
virtual void 처리() = 0;
};
/** "Concrete Abstraction" 1/2 */
class 요리사 : public 사람
{
public:
요리사(작업* i) : pProcess(i) {}
void 처리() override
{
pProcess->처리();
}
private:
작업 * pProcess;
};
/** "Concrete Abstraction" 2/2 */
class 청소부 : public 사람
{
public:
청소부(작업* i) : pProcess(i) {}
void 처리() override
{
pProcess->처리();
}
private:
작업 * pProcess;
};
int main()
{
작업* pCook = new 요리();
작업* pClean = new 청소();
사람* pCooker = new 요리사(pCook);
사람* pCleaner = new 청소부(pClean);
pCooker->처리();
pCleaner->처리();
delete pCook;
delete pClean;
delete pCooker;
delete pCleaner;
return 0;
}
|
예제 결과)
헤드 퍼스트 디자인 패턴:14가지 GoF 필살 패턴! - 프로그래밍 언어 | 쿠팡
쿠팡에서 헤드 퍼스트 디자인 패턴:14가지 GoF 필살 패턴! 구매하고 더 많은 혜택을 받으세요! 지금 할인중인 다른 프로그래밍 언어 제품도 바로 쿠팡에서 확인할 수 있습니다.
www.coupang.com
"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."
'Gof Design Pattern' 카테고리의 다른 글
데코레이터 패턴 (Decorator Pattern) (0) | 2014.01.29 |
---|---|
컴포지트 패턴 (Composite Pattern) (2) | 2014.01.28 |
프록시 패턴 (Proxy Pattern) (0) | 2014.01.27 |
아답터 패턴 (Adapter Pattern) (0) | 2014.01.27 |
빌더 패턴 (Builder Pattern) (0) | 2014.01.26 |