代码改变世界

桥接模式

2010-08-26 22:14  Clingingboy  阅读(303)  评论(0编辑  收藏  举报

        将抽象与实现分离

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();
 }

其实很简单,还是面向对象基础,不小心就成了一个模式