设计模式——桥接模式

名称Bridge
结构 bridge.gif
意图将抽象部分与它的实现部分分离,使它们都可以独立地变化。
适用性
  • 你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。
  • 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时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 )。
Code Example
  1 None.gif //  Bridge
  2 None.gif
  3 None.gif //  Intent: "Decouple an abstraction from its implemntation so that the
  4 None.gif //  two can vary independently". 
  5 None.gif
  6 None.gif //  For further information, read "Design Patterns", p151, Gamma et al.,
  7 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
  8 None.gif
  9 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
 10InBlock.gif * Coupling between classes and class libraries is a major maintenance 
 11InBlock.gif * headache. To ease this problem, often the client talks to an 
 12InBlock.gif * abstraction description, which in turn calls an implementation.
 13InBlock.gif * Sometimes these must evolve - when one changes there can be a need 
 14InBlock.gif * to change the other.  The bridge design pattern lets the abstraction 
 15InBlock.gif * and its implementation evolve separately.  
 16InBlock.gif * 
 17InBlock.gif * So, what is the difference between a bridge and an interface? Interfaces
 18InBlock.gif * can be used when creating bridges - but it should be noted that bridges 
 19InBlock.gif * have additional possibilities. Both the abstraction and the 
 20InBlock.gif * implementation may evolve over time and be the parent of derived classes. 
 21InBlock.gif * The operations needed in the implementation could be defined in an 
 22InBlock.gif * interface if there are no standard methods which are available at the 
 23InBlock.gif * top-level of the implementation.   
 24InBlock.gif * 
 25ExpandedBlockEnd.gif */

 26 None.gif 
 27 None.gif namespace  Bridge_DesignPattern
 28 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 29InBlock.gif    using System;
 30InBlock.gif
 31InBlock.gif    class Abstraction 
 32ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 33InBlock.gif        protected Implementation impToUse;
 34InBlock.gif
 35InBlock.gif        public void SetImplementation(Implementation i)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            impToUse = i;
 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40InBlock.gif        virtual public void DumpString(string str)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            impToUse.DoStringOp(str);                   
 43ExpandedSubBlockEnd.gif        }

 44ExpandedSubBlockEnd.gif    }

 45InBlock.gif
 46InBlock.gif    class DerivedAbstraction_One : Abstraction 
 47ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 48InBlock.gif        override public void DumpString(string str)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            str += ".com";
 51InBlock.gif            impToUse.DoStringOp(str);            
 52ExpandedSubBlockEnd.gif        }
        
 53ExpandedSubBlockEnd.gif    }

 54InBlock.gif
 55InBlock.gif    class Implementation 
 56ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 57InBlock.gif        public virtual void DoStringOp(string str)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            Console.WriteLine("Standard implementation - print string as is");
 60InBlock.gif            Console.WriteLine("string = {0}", str);
 61ExpandedSubBlockEnd.gif        }
        
 62ExpandedSubBlockEnd.gif    }

 63InBlock.gif
 64InBlock.gif    class DerivedImplementation_One : Implementation 
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 66InBlock.gif        override public void DoStringOp(string str)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            Console.WriteLine("DerivedImplementation_One - don't print string");
 69ExpandedSubBlockEnd.gif        }
    
 70ExpandedSubBlockEnd.gif    }

 71InBlock.gif
 72InBlock.gif    class DerivedImplementation_Two : Implementation 
 73ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 74InBlock.gif        override public void DoStringOp(string str)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            Console.WriteLine("DerivedImplementation_Two - print string twice");
 77InBlock.gif            Console.WriteLine("string = {0}", str);
 78InBlock.gif            Console.WriteLine("string = {0}", str);
 79ExpandedSubBlockEnd.gif        }
    
 80ExpandedSubBlockEnd.gif    }

 81InBlock.gif    
 82ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 83InBlock.gif    ///    Summary description for Client.
 84ExpandedSubBlockEnd.gif    /// </summary>

 85InBlock.gif    public class Client
 86ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 87InBlock.gif        Abstraction SetupMyParticularAbstraction() 
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            // we localize to this method the decision which abstraction and
 90InBlock.gif            // which implementation to use. These need to be decided 
 91InBlock.gif            // somewhere and we do it here. All teh rest of the client 
 92InBlock.gif            // code can work against the abstraction object. 
 93InBlock.gif            Abstraction a = new DerivedAbstraction_One();
 94InBlock.gif            a.SetImplementation(new DerivedImplementation_Two());
 95InBlock.gif            return a;
 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif
 98InBlock.gif        public static int Main(string[] args)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{         
100InBlock.gif            Client c = new Client();
101InBlock.gif            Abstraction a = c.SetupMyParticularAbstraction();
102InBlock.gif                
103InBlock.gif            // From here on client code thinks it is talking to the 
104InBlock.gif            // abstraction, and will not need to be changed as 
105InBlock.gif            // derived abstractions are changed. 
106InBlock.gif
107InBlock.gif            // more client code using the abstraction goes here 
108InBlock.gif            // . . . 
109InBlock.gif            a.DumpString("Clipcode");
110InBlock.gif
111InBlock.gif            return 0;
112ExpandedSubBlockEnd.gif        }

113ExpandedSubBlockEnd.gif    }

114ExpandedBlockEnd.gif}

115 None.gif
116 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/06/28/182348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值