桥接模式

博客介绍了桥接模式,它将抽象部分与实现部分分离,使二者可独立变化。这里的实现指抽象类及其派生类用于实现自身的对象。当系统有多角度分类且每种分类可能变化时,将多角度分离可减少耦合,还提及客户端测试类和UML图。

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

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图:

 

转载于:https://www.cnblogs.com/lixianyuan-org/p/9535985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值