This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| design_pattern:bridge_pattern [2017/10/29 04:58] – 만듦 ledyx | design_pattern:bridge_pattern [2021/02/07 03:25] (current) – [Bridge Pattern] ledyx | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| = Bridge Pattern = | = Bridge Pattern = | ||
| - | 2종류의 확장이 혼재하는 프로그램을 **__기능 계층__과 __구현 계층__으로 분리**하고, | + | 2종류의 확장이 혼재하는 프로그램을 **__기능 계층__과 __구현 계층__으로 분리**하고, |
| + | * 클래스 계층의 역할 | ||
| + | * 기능 클래스 계층 | ||
| + | * 상위 클래스는 기본적인 기능을 가지고 있다. | ||
| + | * 하위 클래스에서 **새로운 기능 추가**한다. | ||
| + | * 구현 클래스 계층 | ||
| + | * 상위 클래스는 추상 메소드에 의해 인터페이스(API)를 규정한다. | ||
| + | * 하위 클래스는 구상 메소드에 의해 **인터페이스(API)를 구현**한다. | ||
| + | * 클래스 계층의 혼재와 분리 | ||
| + | * <fc red> | ||
| + | * 언제 쓰지? 장점? | ||
| + | * 예를 들어 어떤 프로그램을 배포하는데 각 OS별로 **의존(구현 계층)**하는 부분이 다른 경우 | ||
| + | * 기능 계층과 구현 계층 따로 확장이 가능하고, | ||
| - | {{tag> | + | {{tag> |
| + | = 기능의 클래스 계층 = | ||
| + | <sxh java ; title: | ||
| + | package func; | ||
| + | import impl.DisplayImpl; | ||
| + | |||
| + | public class Display { | ||
| + | private DisplayImpl impl; // 위임(떠넘기기). Loose Coupling. | ||
| + | |||
| + | public Display(DisplayImpl impl) { | ||
| + | super(); | ||
| + | this.impl = impl; | ||
| + | } | ||
| + | |||
| + | public void open() { | ||
| + | impl.rawOpen(); | ||
| + | } | ||
| + | |||
| + | public void print() { | ||
| + | impl.rawPrint(); | ||
| + | } | ||
| + | |||
| + | public void close() { | ||
| + | impl.rawClose(); | ||
| + | } | ||
| + | |||
| + | public final void display() { | ||
| + | open(); | ||
| + | print(); | ||
| + | close(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: | ||
| + | package func; | ||
| + | |||
| + | import java.util.stream.IntStream; | ||
| + | |||
| + | import impl.DisplayImpl; | ||
| + | |||
| + | public class CountDisplay extends Display { | ||
| + | |||
| + | public CountDisplay(DisplayImpl impl) { | ||
| + | super(impl); | ||
| + | } | ||
| + | |||
| + | public void multiDisplay(int times) { | ||
| + | open(); | ||
| + | IntStream.range(0, | ||
| + | close(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | = 구현의 클래스 계층 = | ||
| + | <sxh java; title: | ||
| + | package impl; | ||
| + | |||
| + | public abstract class DisplayImpl { | ||
| + | public abstract void rawOpen(); | ||
| + | public abstract void rawPrint(); | ||
| + | public abstract void rawClose(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java; title: | ||
| + | package impl; | ||
| + | |||
| + | import java.util.stream.IntStream; | ||
| + | |||
| + | public class StringDisplayImpl extends DisplayImpl { | ||
| + | |||
| + | private String str; | ||
| + | private int width; | ||
| + | |||
| + | public StringDisplayImpl(String str) { | ||
| + | this.str = str; | ||
| + | this.width = str.getBytes().length; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void rawOpen() { | ||
| + | printLine(); | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void rawPrint() { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void rawClose() { | ||
| + | printLine(); | ||
| + | } | ||
| + | |||
| + | private void printLine() { | ||
| + | System.out.print(" | ||
| + | IntStream.range(0, | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | = Client = | ||
| + | <sxh java> | ||
| + | import func.CountDisplay; | ||
| + | import func.Display; | ||
| + | import impl.StringDisplayImpl; | ||
| + | |||
| + | import func.CountDisplay; | ||
| + | import func.Display; | ||
| + | import impl.StringDisplayImpl; | ||
| + | |||
| + | public class Main { | ||
| + | public static void main(String[] args) { | ||
| + | Display d1 = new Display(new StringDisplayImpl(" | ||
| + | CountDisplay d2 = new CountDisplay(new StringDisplayImpl(" | ||
| + | |||
| + | d1.display(); | ||
| + | System.out.println(); | ||
| + | d2.display(); | ||
| + | d2.multiDisplay(3); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | /* | ||
| + | +-------------+ | ||
| + | |Hello, Korea.| | ||
| + | +-------------+ | ||
| + | |||
| + | +------------+ | ||
| + | |Hello, World| | ||
| + | +------------+ | ||
| + | +------------+ | ||
| + | |Hello, World| | ||
| + | |Hello, World| | ||
| + | |Hello, World| | ||
| + | +------------+ | ||
| + | */ | ||
| + | </ | ||