Design mode of bridge pattern structure
Defined:
- Separate the abstract and implementation sections so that they run independently.
- Avoid using inheritance to cause the number of system classes to burst, consider bridging mode.
- Bridging mode translates inheritance relationships into associations, reduces coupling, and reduces the amount of code.
For example:
Public Interface Shape { publicvoid bepaint (String color);}
Public Abstract class Color { shape shape; Public void setshape (Shape shape) { this. Shape = shape; } Public Abstract void draw ();}
Public class extends Color { @Override publicvoid Draw () { shape.bepaint ("Red");} }
Public class extends Color { @Override publicvoid Draw () { shape.bepaint (" Green ");} }
Public class Implements shape { @Override The circle of the publicvoid bepaint ( String color) {+ "); }}
Public class Implements Shape { @Override publicvoid bepaint (String color) { + " Square ");} }
Public classTest {/** * @paramargs*/ Public Static voidMain (string[] args) {Shape circle_shape=NewCircle (); Color Red_color=NewRed (); Red_color.setshape (Circle_shape); Red_color.draw (); System.out.println ("-----------------"); Shape Square_shape=NewSquare (); Red_color.setshape (Square_shape); Red_color.draw (); }}
Experimental results:
Red circle ----------------- Red Square
Bridge mode pros and Cons: Cons:
Design difficulty is relatively large, to be able to correctly identify the system of independent changes in the dimensions, has limitations.
Advantages:
- It is better to realize the decoupling of the abstract part and the realization part than the implementation scheme of the inheritance;
- Expandable without modification of the original system;
Design mode-Bridging mode (bridge pattern)