码迷,mamicode.com
首页 > 其他好文 > 详细

桥接模式

时间:2018-08-26 10:23:43      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:lock   isp   技术分享   spl   之间   rate   prot   void   display   

    桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。
  抽象与实现分离并不是说让抽象类与其派生类分离。实现值得是抽象类和它的派生类用来实现自己的对象。实现系统可能有多角度分类,每一种分类都有可能变化,那么久吧这种多角度分离出来让他们独立变化,减少他们之间的耦合。

1 //Implementor类
2 public abstract class Implementor {
3     public abstract void operation();
4 }

 

 1 //ConcreteImplementorA派生类
 2 public class ConcreteImplementorA extends Implementor {
 3 
 4     @Override
 5     public void operation() {
 6         // TODO Auto-generated method stub
 7         System.out.println("具体实现A的方法执行");
 8     }
 9 
10 }

 

 1 //ConcreteImplementorB派生类
 2 public class ConcreteImplementorB extends Implementor {
 3 
 4     @Override
 5     public void operation() {
 6         // TODO Auto-generated method stub
 7         System.out.println("具体实现B的方法执行");
 8     }
 9 
10 }

 

 

 1 //Abstraction类
 2 public class Abstraction {
 3     //聚合
 4     protected Implementor implementor;
 5 
 6     public void setImplementor(Implementor implementor) {
 7         this.implementor = implementor;
 8     }
 9     
10     public void operation(){
11         implementor.operation();//使用Implementor中的方法
12     }
13     
14 }

 

1 //RefinedAbstraction类
2 public class RefinedAbstraction extends Abstraction {
3     
4     @Override
5     public void operation() {
6         implementor.operation();
7     }
8 }

客户端测试类:

 1 public class TestClient {
 2     public static void main(String[] args) {
 3         Abstraction ab = new RefinedAbstraction();
 4         ab.setImplementor(new ConcreteImplementorA());
 5         ab.operation();//具体实现A的方法执行
 6         
 7         ab.setImplementor(new ConcreteImplementorB());
 8         ab.operation();//具体实现B的方法执行
 9     }
10 }

 

UML图:

技术分享图片

 

桥接模式

标签:lock   isp   技术分享   spl   之间   rate   prot   void   display   

原文地址:https://www.cnblogs.com/lixianyuan-org/p/9535985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!