结构型设计模式(2):桥接模式

动机
有些时候一个抽象应该有不同的实现,比如,保存数据时有两种方式,一种是文件方式,一种是数据库方式,通常的做法是继承保存数据的类,然后实现不同的保存方式。这样做的问题就是难于修改和扩展保存方式,运行时无法切换保存方式。

定义
桥接模式是软件设计模式中最复杂的模式之一,它将事物的抽象部分与它的实现部分分离,使它们都可以独立地变化。

如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。

实现

1. Abstraction - 定义抽象方法。
2. AbstractionImpl - 使用实现接口来实现抽象方法。
3. Implementor - 为具体实现行为定义接口。
4. ConcreteImplementor1, ConcreteImplementor2 - 实现Implementor接口。
/** "Implementor" */
interface DrawingAPI
{
    public void drawCircle(double x, double y, double radius);
}
 
/** "ConcreteImplementor" 1/2 */
class DrawingAPI1 implements DrawingAPI
{
   public void drawCircle(double x, double y, double radius) 
   {
        System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
   }
}
 
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2 implements DrawingAPI
{
   public void drawCircle(double x, double y, double radius) 
   { 
        System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
   }
}
 
/** "Abstraction" */
interface Shape
{
   public void draw();                                            // low-level
   public void resizeByPercentage(double pct);     // high-level
}
 
/** "Refined Abstraction" */
class CircleShape implements Shape
{
   private double x, y, radius;
   private DrawingAPI drawingAPI;
   public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI)
   {
       this.x = x;  this.y = y;  this.radius = radius; 
       this.drawingAPI = drawingAPI;
   }
 
   // low-level i.e. Implementation specific
   public void draw()
   {
        drawingAPI.drawCircle(x, y, radius);
   }   
   // high-level i.e. Abstraction specific
   public void resizeByPercentage(double pct)
   {
        radius *= pct;
   }
}
 
/** "Client" */
class BridgePattern {
   public static void main(String[] args)
   {
       Shape[] shapes = new Shape[2];
       shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());
       shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());
 
       for (Shape shape : shapes)
       {
           shape.resizeByPercentage(2.5);
           shape.draw();
       }
   }
}

实例
1. 动机里面提到的数据保存。
2. 图形的绘制框架。类似上面代码中的实现。

适用场景
1. 你不希望抽象和实现有固定的关系,希望可以在运行时修改实现的方式。
2. 抽象和实现部分都可以独立的扩展,而不相互影响。

参考
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值