Ch 9 Bridge Pattern Dislpay impl open print
Ch. 9 Bridge Pattern Dislpay -impl +open +print +close Display. Impl +raw. Open +raw. Print +raw. Close +display Count. Display +multi. Display String. Display. Impl +raw. Open +raw. Print +raw. Close 기능적 class 계층 장덕성 구현적 class 계층 계명대학교 컴퓨터공학과 정보공학실험실
Ch. 9 Bridge Pattern ▌Display Class public class Display { private Display. Impl impl; public Display(Display. Impl impl) { // 구현한 class의 instance를 impl로 받음 this. impl = impl; } public void open() { impl. raw. Open(); // raw. Open()의 구현을 string. Display. Impl에서 처리 } public void print() { impl. raw. Print(); } public void close() { impl. raw. Close(); } public final void display() { open(); print(); close(); } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실
Ch. 9 Bridge Pattern ▌Count. Display Class public class Count. Display extends Display { public Count. Display(Display. Impl impl) { // 상위 class와 같이 // bridge 역할을 하는 field super(impl); } public void multi. Display(int times){ // open()과 close()로 // 틀을 만들고 // string을 times 만큼 출력 open(); for (int i = 0; I < times; i++) { print(); } close(); } ▌Display. Impl Class public abstract class Display. Impl { public abstract void raw. Open(); public abstract void raw. Print(); public abstract void raw. Close(); } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실
Ch. 9 Bridge Pattern ▌ String. Display. Impl Class public class String. Display. Impl extends Display. Impl { private String string; // 표시해야할 문자열 private int width; // 바이트 단위로 계산한 문자열의 "길이" String. Display. Impl(String string) { this. string = string; this. width = string. get. Bytes(). length; } public void raw. Open() { print. Line(); } public void raw. Print() { System. out. println("|" + string + "|"); // 앞뒤에 "|"를 붙여서 표시 } public void raw. Close() { print. Line(); } private void print. Line() { // 모서리에 ‘+’와 width 개의 ‘-’를 표시 System. out. print("+"); for (int i = 0; i < width; i++) { System. out. print("-"); } public System. out. println("+"); } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실
Ch. 9 Bridge Pattern ▌Main Class public class Main { public static void main(String[] args) { Display d 1 = new Display(new String. Display. Impl("Hello, Korea. ")); Display d 2 = new Count. Display(new String. Display. Impl("Hello, World. ")); Count. Display d 3 = new Count. Display(new String. Display. Impl("Hello, Universe. ")); d 1. display(); d 2. display(); d 3. multi. Display(5); } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실
- Slides: 5