原文:http://www.linkedkeeper.com/detail/blog.action?bid=26

You are here:  架构&实践 - 设计模式
Frank     2015/08/18    阅读: 350    评论: 0    收藏: 0
 

定义将抽象部分与实现部分分离,使它们都可以独立的变化。结构示例Implementor:定义实现接口interface Implementor { // 实现抽象部分需要的某些具体功能 public void operationImpl();}Abstraction:定义抽象接口abstract class Abstraction { // 持有一个 Implementor 对象,...

定义

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

结构

示例

Implementor:定义实现接口

1
2
3
4
interface Implementor {
    // 实现抽象部分需要的某些具体功能
    public void operationImpl();
}

Abstraction:定义抽象接口

1
2
3
4
5
6
7
8
9
10
11
12
13
abstract class Abstraction {
    // 持有一个 Implementor 对象,形成聚合关系
    protected Implementor impl;
 
    public Abstraction(Implementor impl) {
        this.impl = impl;
    }
 
    // 可能需要转调实现部分的具体实现
    public void operation() {
        impl.operationImpl();
    }
}

ConcreteImplementor:实现 Implementor 中定义的接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ConcreteImplementorA implements Implementor {
    @Override
    public void operationImpl() {
        // 真正的实现
        System.out.println("具体的实现A");
    }
}
 
class ConcreteImplementorB implements Implementor {
    @Override
    public void operationImpl() {
        // 真正的实现
        System.out.println("具体的实现B");
    }
}

RefinedAbstraction:扩展 Abstraction 类

1
2
3
4
5
6
7
8
9
10
11
12
class RefinedAbstraction extends Abstraction {
 
    public RefinedAbstraction(Implementor impl) {
        super(impl);
    }
 
    public void otherOperation() {
        // 实现一定的功能,可能会使用具体实现部分的实现方法,
        // 但是本方法更大的可能是使用 Abstraction 中定义的方法
        // 通过组合使用 Abstraction  中定义的方法来完成更多的功能。
    }
}

测试代码

1
2
3
4
5
6
7
8
public class BridgePattern {
    public static void main(String[] args) {
        Implementor impl = new ConcreteImplementorA();
        RefinedAbstraction abs = new RefinedAbstraction(impl);
        abs.operation();
        abs.otherOpertaion();
    }
}

样例

实现部分定义接口

1
2
3
4
interface MessageImplementor {
    // 发送消息
    public void send(String message);
}

抽象部分定义接口

1
2
3
4
5
6
7
8
9
10
11
12
public abstract class AbstractMessage {
    // 持有一个实现部分的对象
    protected MessageImplementor impl;
    // 构造方法,传入实现部分的对象
    public AbstractMessage(MessageImplementor impl) {
        this.impl = impl;
    }
    // 发送消息,转调实现部分的方法
    public void sendMessage(String message) {
        this.impl.send(message);
    }
}

具体的实现发送消息

1
2
3
4
5
6
7
8
9
10
11
public class MessageSMS implements MessageImplementor {
    public void send(String message) {
        System.out.prinlt("使用短信方式发送消息:" + message);
    }
}
 
public class MessageEmail implements MessageImplementor {
    public void send(String message) {
        System.out.println("使用Email方法发送消息:" + message);
    }
}

抽象的消息消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CommonMessage extends AbstractMessage {
 
    public CommonMessage(MessageImplementor impl) {
        super(impl);
    }
 
    public void sendMessage(Stirng message) {
        super.sendMessage(message);
    }
}
 
public class UrgencyMessage extends AbstractMessage {
 
    public UrgencyMessage (MessageImplementor impl) {
        super(impl);
    }
 
    public void sendMessage(Stirng message) {
        super.sendMessage(message);
    }
}

转载请并标注:

“本文转载自 http://www.linkedkeeper.com/detail/blog.action?bid=26 (文/Frank)”

Bridge 设计模式的更多相关文章

  1. 【设计模式】Bridge

    前言 Bridge设计模式,将一个复杂类分成可以单独开发的部分.分成的两个部分,abstraction,implementation.字面上是抽象和实现,但不同于抽象方法及其实现.下面摘录Wiki的两 ...

  2. 连载:面向对象的葵花宝典:思考、技巧与实践(39) - 设计原则 vs 设计模式

    它的设计原则,和设计模式,是否该用它? ============================================================================= 在& ...

  3. Reactor构架模式

    http://www.cnblogs.com/hzbook/archive/2012/07/19/2599698.html Reactor框架是ACE各个框架中最基础的一个框架,其他框架都或多或少地用 ...

  4. 如何写出好的Java代码?

    1. 优雅需要付出代价.从短期利益来看,对某个问题提出优雅的解决方法,似乎可能花你更多的时间.但当它终于能够正确执行并可轻易套用于新案例中,不需要花上数以时计,甚至以天计或以月计的辛苦代价时,你会看得 ...

  5. Backbone.js的技巧和模式

    Backbone.js的技巧和模式 Backbone.js的技巧和模式   本文由白牙根据Phillip Whisenhunt的<Backbone.js Tips And Patterns> ...

  6. [源码解析]PyTorch如何实现前向传播(2) --- 基础类(下)

    [源码解析]PyTorch如何实现前向传播(2) --- 基础类(下) 目录 [源码解析]PyTorch如何实现前向传播(2) --- 基础类(下) 0x00 摘要 0x01 前文回顾 0x02 Te ...

  7. Net设计模式实例之桥接模式( Bridge Pattern)

    一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...

  8. C++设计模式-Bridge桥接模式

    作用:将抽象部份与它的实现部份分离,使它们都可以独立地变化. 将抽象(Abstraction)与实现(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式 ...

  9. 设计模式学习之桥接模式(Bridge,结构型模式)(15)

    参考地址:http://terrylee.cnblogs.com/archive/2006/02/24/336652.html 概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化, ...

随机推荐

  1. 初识beego

    beego是一个基于golang的web框架,这里记录些使用中碰到的东西. 输出: this.Ctx.Output.Write([]byte("test")) //这里是作为res ...

  2. [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 后面forIndexPath:indexPath参数的解释

    解决以下错误: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'u ...

  3. php的memcache和memcached扩展区别【转载】

    老生长谈的问题了.我这里就整理一下. memcache的文档在:http://pecl.php.net/package/memcache memcached的文档在:http://pecl.php.n ...

  4. gnome3

    http://askubuntu.com/questions/67753/how-do-i-add-an-application-to-the-dash https://wiki.gnome.org/ ...

  5. Outing

    Outing 题目描述 Organising a group trip for the elderly can be a daunting task... Not least because of t ...

  6. WPF中静态引用资源与动态引用资源的区别

    WPF中静态引用资源与动态引用资源的区别   WPF中引用资源分为静态引用与动态引用,两者的区别在哪里呢?我们通过一个小的例子来理解. 点击“Update”按钮,第2个按钮的文字会变成“更上一层楼”, ...

  7. PHP商品倒计时 php实现当前用户在线人数

    //PHP商品倒计时 date_default_timezone_set("Asia/shanghai");//地区 //配置每天的活动时间段 $starttimestr = &q ...

  8. C语言客户端服务器代码

    /* sockclnt.c*/ #include <stdio.h>#include <string.h>#include <stdlib.h>#include & ...

  9. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  10. Illegal resource reference: @*android resources are private and not always present

    0:前言 在android开发中,当使用别人的代码的时候,在style.xml中有此种错误 1:解决方案 删除*星号