1 /** 
   2  * BridgePattern Example
   3  */
   4 
   5 class Display {
   6     private DisplayImpl impl;
   7     public Display(DisplayImpl impl) {
   8         this.impl = impl;
   9     }
  10     public void open() {
  11         impl.rawOpen();
  12     }
  13     public void print() {
  14         impl.rawPrint();
  15     }
  16     public void close() {
  17         impl.rawClose();
  18     }
  19     public final void display() {
  20         open();
  21         print();
  22         close();
  23     }
  24 }
  25 
  26 class CountDisplay extends Display {
  27     public CountDisplay(DisplayImpl impl) {
  28         super(impl);
  29     }
  30     public void multiDisplay(int times) {
  31         open();
  32         for(int i=0; i < times; i++) {
  33             print();
  34         }
  35         close();
  36     }
  37 }
  38 
  39 abstract class DisplayImpl {
  40     public abstract void rawOpen();
  41     public abstract void rawPrint();
  42     public abstract void rawClose();
  43 }
  44 
  45 class StringDisplayImpl extends DisplayImpl {
  46     private String string;
  47     private int width;
  48     public StringDisplayImpl(String string) {
  49         this.string = string;
  50         this.width = string.getBytes().length;
  51     }
  52     public void rawOpen() {
  53         printLine();
  54     }
  55     public void rawPrint() {
  56         System.out.println("|"+string+"|");
  57     }
  58     public void rawClose() {
  59         printLine();
  60     }
  61     private void printLine() {
  62         System.out.print("+");
  63         for(int i=0; i < width; i++) {
  64             System.out.print("-");
  65         }
  66         System.out.println("+");
  67     }
  68 }
  69 
  70 public class BridgeDisplay {
  71     public static void main(String[] args) {
  72         Display d1 = new Display(new StringDisplayImpl("Hello, Korea."));
  73         Display d2 = new CountDisplay(new StringDisplayImpl("Hello, World."));
  74         CountDisplay d3 = new CountDisplay(new StringDisplayImpl(
  75                     "Hello, Universe."));
  76         d1.display();
  77         d2.display();
  78         d3.display();
  79         d3.multiDisplay(5);
  80     }
  81 }
web biohackers.net