Bridging mode-bridge
The bridge pattern is intended to "decouple abstraction (abstraction) from implementation (implementation), separating the functional hierarchy of a class from the implementation hierarchy of a class into two separate class hierarchies."
Class implementation hierarchy Interface Displayimpl interface
Public interface Displayimpl { void Rawopen (); void Rawprint (); void Rawclose ();}
Stringdisplayimpl class
public class Stringdisplayimpl implements 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 ("|" + String + "|"); public void Rawclose () { printline (); } private void PrintLine () { System.out.print ("+"); for (int i = 0; i < width; i++) { System.out.print ("-"); } System.out.println ("+");} }
Class's functional hierarchy display class
The Displayimpl interface is associated at the highest level of the feature implementation, where it is bridged.
public 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 (); }}
CountDisplay class
On the basis of the display class, the function is extended. Extend a multidisplay () feature.
public class CountDisplay extends Display {public countdisplay (Displayimpl impl) { super (IMPL); } public void Multidisplay (int times) { open (); for (int i = 0; I < times; i++) { print (); } Close (); }}
Main
For Test runs
public class Main {public static void Main (string[] args) { display D1 = new display (New Stringdisplayimpl ("Hello , China. ")); Display D2 = new CountDisplay (New Stringdisplayimpl ("Hello, World")); CountDisplay d3 = new CountDisplay (New Stringdisplayimpl ("Hello, Universe.")); D1.display (); D2.display (); D3.display (); D3.multidisplay (5);} }
Bridging mode-bridge (Java implementation)