将抽象与实现分离
class Abstraction { Bridge bridge; public Abstraction (Bridge implementation) { bridge = implementation; } public string Operation () { return "Abstraction" +" <<< BRIDGE >>>> "+bridge.OperationImp(); } } interface Bridge { string OperationImp(); }
在构造函数中传入接口,然后在Operation方法中调用接口方法
class ImplementationA : Bridge { public string OperationImp () { return "ImplementationA"; } } class ImplementationB : Bridge { public string OperationImp () { return "ImplementationB"; } }
客户端调用
static void Main () { Console.WriteLine("Bridge Pattern\n"); Console.WriteLine(new Abstraction (new ImplementationA()).Operation()); Console.WriteLine(new Abstraction (new ImplementationB()).Operation()); Console.ReadKey(); }