본문 바로가기
Spring/DesighPattern

GOF패턴 - BridgePattern(구조)

by windy7271 2023. 2. 24.
728x90
반응형

BridgePattern   

어댑터 패턴과 연결해서 이해해야한다.

멤버변수를 통해서 기능을 동작하는 패턴

 

기능부분만 바꾼다. dafult, sound

 

Abstraction

implemetor

refindAbstraction

concretImplementor

 

package Bridge1;

public class Main {
    public static void main(String[] args) {
        PrintMoresCode code = new PrintMoresCode(new SoundMCF());

//        code.g();code.a();code.r();code.a();code.m();
        code.g().a().r().a().m(); // 체이닝


    }
}

 

package Bridge1;

//abstraction 기능부분
public class MorseCode {
    private MorseCodeFuction fuction;

    public void setFuction(MorseCodeFuction fuction) {
        this.fuction = fuction;
    }

    public MorseCode(MorseCodeFuction fuction) {
        this.fuction = fuction;
    }

    public void dot() {
        fuction.dot();
        System.out.print("."); // 다른곳에서 구현하면 간편하게 된다.
    }
    public void dash() {
        fuction.dash();
        System.out.print("-");
    }

    public void space() {
        fuction.space();
        System.out.print(" ");
    }
}
package Bridge1;

// 기능 부분
public class PrintMoresCode extends MorseCode{

    public PrintMoresCode(MorseCodeFuction fuction) {
        super(fuction);
    }

    public PrintMoresCode g() {
        dash();dash();dot();space();
        return this;
    }

    public PrintMoresCode a() {
        dot();dash();space();
        return this;
    }

    public PrintMoresCode r() {
        dot();dash();dash();space();
        return this;
    }

    public PrintMoresCode m() {
        dash();dash();space();
        return this;
    }
}
package Bridge1;

// implementor
public interface MorseCodeFuction {

    public void dot();
    public void dash();
    public void space();

}
package Bridge1;

//concreteimplemetor
public class DefaultMCF implements MorseCodeFuction {

    @Override
    public void dot() {
        System.out.print(".");
    }

    @Override
    public void dash() {
        System.out.print("-");
    }

    @Override
    public void space() {
        System.out.print(" ");
    }
}

package Bridge1;

//concreteimplemetor
public class SoundMCF implements MorseCodeFuction {

    @Override
    public void dot() {
        System.out.print("점");
    }

    @Override
    public void dash() {
        System.out.print("선");
    }

    @Override
    public void space() {
        System.out.print(" ");
    }
}
반응형

댓글