java设计模式--结构型模式--桥接模式

 1                                     桥接模式
 2  概述
 3     将抽象部分与它的实现部分分离,使它们都可以独立地变化。
 4  
 5  
 6  适用性
 7     1.你不希望在抽象和它的实现部分之间有一个固定的绑定关系。
 8       例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。
 9 
10     2.类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。
11       这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
12 
13     3.对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
14 
15     4.正如在意图一节的第一个类图中所示的那样,有许多类要生成。
16       这样一种类层次结构说明你必须将一个对象分解成两个部分。
17 
18     5.你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。
19  
20  
21  参与者
22     1.Abstraction
23       定义抽象类的接口。
24       维护一个指向Implementor类型对象的指针。
25 
26     2.RefinedAbstraction
27       扩充由Abstraction定义的接口。
28 
29     3.Implementor
30       定义实现类的接口,该接口不一定要与Abstraction的接口完全一致。
31       事实上这两个接口可以完全不同。
32       一般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。
33 
34     4.ConcreteImplementor
35       实现Implementor接口并定义它的具体实现。

 

测试类:

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         
 5         Person man = new Man();
 6         
 7         Person lady = new Lady();
 8         
 9         Clothing jacket = new Jacket();
10         
11         Clothing trouser = new Trouser();
12         
13         jacket.personDressCloth(man);
14         trouser.personDressCloth(man);
15 
16         jacket.personDressCloth(lady);
17         trouser.personDressCloth(lady);
18     }
19 }

 

 1 public abstract class Person {
 2 
 3     private Clothing clothing;
 4     
 5     private String type;
 6 
 7     public Clothing getClothing() {
 8         return clothing;
 9     }
10 
11     public void setClothing() {
12         this.clothing = ClothingFactory.getClothing();
13     }
14     
15     public void setType(String type) {
16         this.type = type;
17     }
18     
19     public String getType() {
20         return this.type;
21     }
22     
23     public abstract void dress();
24 }

 

1 public abstract class Clothing {
2 
3     public abstract void personDressCloth(Person person);
4 }

 

 1 public class Lady extends Person {
 2 
 3     public Lady() {
 4         setType("女人");
 5     }
 6     
 7     public void dress() {
 8         Clothing clothing = getClothing();
 9         clothing.personDressCloth(this);
10     }
11 }

 

1 public abstract class Clothing {
2 
3     public abstract void personDressCloth(Person person);
4 }

 

1 public class Jacket extends Clothing {
2 
3     public void personDressCloth(Person person) {
4         System.out.println(person.getType() + "穿马甲");
5     }
6 }

 

 1 @SuppressWarnings("static-access")
 2 public class ClothingFactory {
 3     private static Clothing clothing;
 4     
 5     public ClothingFactory(Clothing clothing){
 6         this.clothing = clothing;
 7     }
 8     
 9     public static Clothing getClothing() { 
10         return clothing;
11     }
12 
13 }

 

1 public class Trouser extends Clothing {
2 
3     public void personDressCloth(Person person) {
4         System.out.println(person.getType() + "穿裤子");
5     }
6 }

 

posted @ 2014-10-23 12:43  全力以赴001  阅读(310)  评论(0编辑  收藏  举报