结构型设计模式之桥接模式(Bridge)

结构
意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化。
适用性
  • 你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。
  • 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时B r i d g e 模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
  • 对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
  • (C + +)你想对客户完全隐藏抽象的实现部分。在C + +中,类的表示在类接口中是可见的。
  • 有许多类要生成。这样一种类层次结构说明你必须将一个对象分解成两个部分。R u m b a u g h 称这种类层次结构为“嵌套的普化”(nested generalizations )。
  • 你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。一个简单的例子便是C o p l i e n 的S t r i n g 类[ C o p 9 2 ],在这个类中多个对象可以共享同一个字符串表示(S t r i n g R e p )。
 1 using System;
 2 
 3     class Abstraction 
 4     {
 5         protected Implementation impToUse;
 6 
 7         public void SetImplementation(Implementation i)
 8         {
 9             impToUse = i;
10         }
11 
12         virtual public void DumpString(string str)
13         {
14             impToUse.DoStringOp(str);                   
15         }
16     }
17 
18     class DerivedAbstraction_One : Abstraction 
19     {
20         override public void DumpString(string str)
21         {
22             str += ".com";
23             impToUse.DoStringOp(str);            
24         }        
25     }
26 
27     class Implementation 
28     {
29         public virtual void DoStringOp(string str)
30         {
31             Console.WriteLine("Standard implementation - print string as is");
32             Console.WriteLine("string = {0}", str);
33         }        
34     }
35 
36     class DerivedImplementation_One : Implementation 
37     {
38         override public void DoStringOp(string str)
39         {
40             Console.WriteLine("DerivedImplementation_One - don't print string");
41         }    
42     }
43 
44     class DerivedImplementation_Two : Implementation 
45     {
46         override public void DoStringOp(string str)
47         {
48             Console.WriteLine("DerivedImplementation_Two - print string twice");
49             Console.WriteLine("string = {0}", str);
50             Console.WriteLine("string = {0}", str);
51         }    
52     }
53     
54     /// <summary>
55     ///    Summary description for Client.
56     /// </summary>
57     public class Client
58     {
59         Abstraction SetupMyParticularAbstraction() 
60         {
61             // we localize to this method the decision which abstraction and
62             // which implementation to use. These need to be decided 
63             // somewhere and we do it here. All teh rest of the client 
64             // code can work against the abstraction object. 
65             Abstraction a = new DerivedAbstraction_One();
66             a.SetImplementation(new DerivedImplementation_Two());
67             return a;
68         }
69 
70         public static int Main(string[] args)
71         {         
72             Client c = new Client();
73             Abstraction a = c.SetupMyParticularAbstraction();
74                 
75             // From here on client code thinks it is talking to the 
76             // abstraction, and will not need to be changed as 
77             // derived abstractions are changed. 
78 
79             // more client code using the abstraction goes here 
80             // . . . 
81             a.DumpString("Clipcode");
82 
83             return 0;
84         }
85     }
桥接模式
原文地址:https://www.cnblogs.com/ziranquliu/p/4650181.html