`
hulianwang2014
  • 浏览: 694157 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

桥接模式(Bridge Pattern)

 
阅读更多

从类的实现中分离出抽象或者接口类,以便于这两个类能够互不相干,这种模式称作桥接模式。这种类型模式属于结构设计模式之一, 它通过提供了一个桥结构来分离具体的实现类和抽象类。

适用场合和优势:

  1. 想永久地分离抽象和实现类。
  2. 在多个对象中分享一个具体的实现类。
  3. 想去提高可扩展性。
  4. 从客户端哪里隐藏具体的实现。
具体分析一个简单的实例,看看它是如何实现桥接模式。
我们有一个接口DrawAPI, 桥接模式的实现类和它的之类RedCircle,GreenCircle。Shape是一个抽象类,适用了DreawAPI类的实例对象。BridgePatternDemo作为测试类,测试有关内容。UML类如下:


drawAPI接口类。
public interface DrawAPI {
    public void drawCircle(int radius, int x, int y);
}

它的子类RedCircle、GreedCircle。
public class RedCircle implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: "
                              + radius +", x: " +x+", "+ y +"]");
    }
}

public class GreenCircle implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: green, radius: "
                + radius +", x: " +x+", "+ y +"]");
    }
}
定义一个抽象类Shape。
public abstract class Shape {
    protected DrawAPI drawAPI;
    protected Shape(DrawAPI drawAPI){
        this.drawAPI = drawAPI;
    }
    public abstract void draw();
}

抽象类的子类.
public class Circle extends Shape {
    private int x, y, radius;

    public Circle(int x, int y, int radius, DrawAPI drawAPI) {
        super(drawAPI);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public void draw() {
        drawAPI.drawCircle(radius,x,y);
    }
}

测试代码如下:
public class BridgePatternDemo {
    public static void main(String[] args) {
        Shape redCircle = new Circle(100,100, 10, new RedCircle());
        Shape greenCircle = new Circle(100,100, 10, new GreenCircle());

        redCircle.draw();
        greenCircle.draw();
    }
}

测试结果:
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]


分享到:
评论

相关推荐

    设计模式之桥接模式BridgePattern

    将抽象部分与它的实现部分分离,使它们都可以独立地变化。

    设计模式面面观(10):桥接模式(Bridge Pattern)-结构型模式

    创建型模式 (100%) 设计模式面面观(8):创建型模式总结 (100%) 设计模式面面观(9):适配器模式(Adapter Pattern)-结构型模式 (100%) 设计模式面面观(10):桥接模式(Bridge Pattern)-结构型模式 ...

    桥接模式(Bridge Pattern)原理图

    桥接模式是一种结构型设计模式,它的目的是将抽象化与实现化解耦,使得它们可以独立变化。这种模式主要用于处理当一个类存在两个或多个独立的变化的维度时的情况,例如,当一个类的抽象部分和实现部分可能发生变化,...

    Bridge 桥接模式(结构型模式)

    -C#面向对象设计模式纵横谈(8):Bridge 桥接模式(结构型模式)

    .NET设计模式(9):桥接模式(BridgePattern)

    [GOF《设计模式》]图1Bridge模式结构图桥接模式将抽象部分与它的实现分离,使它们能够独立地变化。一个普通的开关控制的电灯、电风扇等等,都是桥接的例子。开关的目的是将设备打开或关闭。实际的开关可以是简单的双...

    BridgePattern 桥接设计模式示例

    设计模式 桥接设计模式示例

    Python设计模式之桥接模式原理与用法实例分析

    桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化. 下面是一个桥接模式的demo: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Andy' 大话设计模式 设计模式——...

    Java设计模式之桥接模式

    桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使它们都可以独立地变化。更容易理解的表述是:实现系统可从多种维度分类,桥接模式将各维度抽象出来,各维度独立变化,之后可通过聚合,将各维度组合...

    C#版 24种设计模式

    观察者模式(Observer Pattern) 建造者模式(Builder Pattern) 解释器模式(Interpreter Pattern) 命令模式(Command Pattern) 模板方法模式(Template Method Pattern) 桥接模式(Bridge Pattern) 适配器模式(Adapter ...

    Java设计模式,并加上个人理解

    1. 设计模式 1.1 含义 1.2 作用 1.3 设计原则 1.4 分类 2. 简单工厂模式 (SimpleFactoryPattern) 3. 工厂方法模式 (Factory Method) ...16. 桥接模式 (Bridge Pattern) 17. 观察者模式 (Observer Pattern)

    C#设计模式_设计模式_C#_

    桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法...

    用Java实现23种设计模式

    桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式...

    C#设计模式(23种设计模式)

    桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板...

    Bridge Pattern

    桥接模式,参考自 http://www.cnblogs.com/houleixx/archive/2008/02/23/1078877.html

    设计模式PPT

     桥接模式(Bridge Pattern)  组合模式(Composite Pattern)  装饰者模式(Decorator Pattern)  外观模式(Facade Pattern)  享元模式(Flyweight Pattern)  代理模式(Proxy Pattern) 行为型模式...

    设计模式代码——c#

    7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型 ...

    23种设计模式 (创建型,结构型,行为型)

    桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板...

    33种JAVA设计模式DEMO

    桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy...

    32种设计模式

    桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy ...

Global site tag (gtag.js) - Google Analytics