본문 바로가기
Back-end/Java

[디자인 패턴] Bridge Pattern

by JiGyeong 2019. 8. 21.

다른이름

Handle/Body Pattern

 

키워드

기능부분과 구현부분을 분리

 

활용

1. 런타임마다의 구현방법을 변경하고 싶을 때

2. 구현 방식을 은닉하고 싶을 때. ( 별도의 클래스에 구현, 정의 하고 private로 감추는 경우 )

3. 구현과 기능이 각각 Hierarchy를 가질 때

 

방법

Abstraction : 기능 클래스

Implementor :  구현 클래스

 

* Abstraction에서 Implementor를 impl로 멤버변수로 가지고 있으면서 사용

(impl : 기능 클래스와 구현 클래스 두 계층의 다리가 됨. impl 변수로 구현기능 사용)

 

구현 결과

Main.java

*abstraction

ㄴ MorseCode.java

ㄴ PrintMorseCode.java

*implementor

ㄴ MorseCodeImpl.java

ㄴ MorseCodeDefaultImpl.java

ㄴ MorseCodeSoundImpl.java

 

abstraction

* MorseCode.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package structure.bridge.abstraction;
 
import structure.bridge.implementor.MorseCodeImpl;
 
public abstract class MorseCode {
    
    private MorseCodeImpl impl ;
    
    public MorseCode(MorseCodeImpl impl) {
        this.impl = impl;
    }
        
    public void dash(){
        impl.dash();
    }
    public void dot(){
        impl.dot();
    }
    public void space(){
        impl.space();
    }
    
}
 
cs

 

 

 

* PrintMorseCode.java

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
package structure.bridge.abstraction;
 
import structure.bridge.implementor.MorseCodeImpl;
 
public class PrintMorseCode extends MorseCode{
    
    public static PrintMorseCode newInstance(MorseCodeImpl impl) {
        return new PrintMorseCode(impl);
    }
 
    public PrintMorseCode(MorseCodeImpl impl) {
        super(impl);
    }
    
    public PrintMorseCode B() {
        dash();dot();dot();dot();space();
        return this;
    }
    public PrintMorseCode F() {
        dot();dot();dash();dot();space();
        return this;
    }
    public PrintMorseCode I() {
        dot();dot();space();
        return this;
    }
 
}
 
cs

 

implementor

* MorseCodeImpl.java

1
2
3
4
5
public interface MorseCodeImpl {
    public void dot();
    public void dash();
    public void space();
}
cs

 

* MorseCodeDefaultImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MorseCodeDefaultImpl implements MorseCodeImpl{
 
    public static MorseCodeDefaultImpl newInstance() {
        return new MorseCodeDefaultImpl();
    }
    
    public void dot() {
        System.out.print(".");
    }
 
    public void dash() {
        System.out.print("_");
    }
 
    public void space() {
        System.out.print("  ");
    }
 
}
cs

 

* MorseCodeSoundImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MorseCodeSoundImpl implements MorseCodeImpl{
 
    public static MorseCodeSoundImpl newInstance() {
        return new MorseCodeSoundImpl();
    }
    
    public void dot() {
        System.out.print("Bi");
    }
 
    public void dash() {
        System.out.print("Beee");
    }
 
    public void space() {
        System.out.print("  ");
    }
 
}
cs

 

Main.java

1
2
3
4
5
6
7
8
9
10
11
import structure.bridge.abstraction.PrintMorseCode;
import structure.bridge.implementor.MorseCodeDefaultImpl;
import structure.bridge.implementor.MorseCodeSoundImpl;
 
public class Main {
    public static void main(String[] args) {
        MorseCodeImpl impl = MorseCodeDefaultImpl.newInstance();
        PrintMorseCode pmc = PrintMorseCode.newInstance(impl);
        pmc.B().F().I();
    }
}
cs

'Back-end > Java' 카테고리의 다른 글

ERROR Could not generate DH Keypair  (0) 2021.02.19
[디자인패턴] Observer Pattern  (0) 2019.09.10
델리게이션 구조란?  (0) 2019.06.10
[JAVA] Thread  (2) 2016.06.10
[Java] SingleTon 기본 static  (0) 2016.04.28