Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45457969
I. Overview
Separate the abstract part from its implementation, so that they can all change independently.
Second, applicability
1. You do not want to have a fixed binding relationship between the abstraction and its implementation section. For example, this may be because the implementation part of the program run time should be selectable or switched.
2. The abstraction of a class and its implementation should be augmented by methods that generate subclasses. Bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
3. Modifications to an abstract implementation section should not have an impact on the customer, i.e. the customer's code does not have to be recompiled.
4. As shown in the first class diagram in the intent section, there are many classes to build. Such a class hierarchy illustrates that you must break up an object into two parts. 5. You want to share the implementation across multiple objects (you might use reference counting), but at the same time ask the customer not to know that.
Third, participants
1.Abstraction
Defines an interface for an abstract class. Maintains a pointer to an Implementor type object.
2.RefinedAbstraction
Expands the interface defined by abstraction.
3.Implementor
Defines an interface for the implementation class that does not have to be exactly the same as the abstraction interface. In fact, the two interfaces can be completely different. Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations.
4.ConcreteImplementor
Implement the Implementor interface and define its specific implementation.
Four, class diagram
V. Examples
Abstraction
Package com.lyz.design.bridge;/** * Define abstraction Person class * @author Liuyazhuang * */public abstract class Person {private Clothing clothing;private String type;public clothing getclothing () {return clothing;} public void setclothing (clothing clothing) {this.clothing = clothing;} public void SetType (String type) {this.type = type;} Public String GetType () {return this.type;} public abstract void Dress ();}
refinedabstraction
Package com.lyz.design.bridge;/** * defines refinedabstraction class man * @author Liuyazhuang * */public class Mans extends person {
public man () { setType ("men"); } public void dress () { Clothing clothing = getclothing (); Clothing.persondresscloth (this);} }
Package com.lyz.design.bridge;/** * defines Refinedabstraction class lady * @author Liuyazhuang * */public class Lady extends person { Public Lady () { setType ("woman"); } public void dress () { Clothing clothing = getclothing (); Clothing.persondresscloth (this);} }
implementor
Package com.lyz.design.bridge;/** * Definition Implementor class clothing * @author Liuyazhuang * */public abstract class Clothing {
public abstract void Persondresscloth (person person);}
Concreteimplementor
Package com.lyz.design.bridge;/** * Definition Concreteimplementor class Jacket * @author Liuyazhuang * */public class Jacket extends Clo thing {public void Persondresscloth (person person) { System.out.println (Person.gettype () + "wear waistcoat");} }
Package com.lyz.design.bridge;/** * Definition Concreteimplementor class Trouser * @author Liuyazhuang * */public class Trouser extends Clothing {public void Persondresscloth (person person) { System.out.println (Person.gettype () + "wear Pants");} }
Test
Package com.lyz.design.bridge;/** * Testing class * @author Liuyazhuang * */public class Test {public static void main (string[] args {Person man = new Man (); Person lady = new lady (); Clothing jacket = new Jacket (); Clothing trouser = new Trouser (), Jacket.persondresscloth (man), Trouser.persondresscloth (man); Jacket.persondresscloth (Lady); Trouser.persondresscloth (Lady);}}
result
Men wear vests, men wear pants, women wear vests, women wear pants.
On the design mode of Java--bridging mode (bridge)