BridgePattern

<textarea cols="50" rows="15" name="code" class="java:showcolumns"> class Display { private DisplayImpl impl; public Display(DisplayImpl impl){ 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(); } }; class CountDisplay extends Display { public CountDisplay(DisplayImpl impl){ super(impl); } public void multiDisplay(int times){ open(); for(int i=0;i &lt; times;i++){ print(); } close(); } }; abstract class DisplayImpl { public abstract void rawOpen(); public abstract void rawPrint(); public abstract void rawClose(); }; class StringDisplayImpl extends DisplayImpl { private String string; private int width; public StringDisplayImpl(String string){ this.string = string; this.width = string.getBytes().length; } public void rawOpen(){ printLine(); } public void rawPrint(){ System.out.println(&quot;|&quot;+string+&quot;|&quot;); } public void rawClose(){ printLine(); } private void printLine(){ System.out.print(&quot;+&quot;); for(int i=0;i&lt;width;i++){ System.out.print(&quot;-&quot;); } System.out.println(&quot;+&quot;); } }; class Main { public static void main(String[] args){ Display d1 = new Display(new StringDisplayImpl(&quot;Hello,Japan.&quot;)); Display d2 = new Display(new StringDisplayImpl(&quot;Hello,World.&quot;)); CountDisplay d3 = new CountDisplay(new StringDisplayImpl(&quot;Hello,Universe&quot;)); d1.display(); d2.display(); d3.display(); d3.multiDisplay(5); } };</textarea>

 

 

你可能感兴趣的:(BridgePattern)