zoukankan      html  css  js  c++  java
  • Bridge 设计模式

    原文: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)”

  • 相关阅读:
    左转弯待转区,什么时候能进,什么时候不能进?
    吵架最激烈不过一分钟,而那一分钟你说出的话,是你用一百分钟都弥补不回来的。
    2017年1月14 15开车总结 英西
    2016年12月17 18 练车总结
    有时候为了方便sql语句的对比和查询,我们可以使用declare来定义变量 上下篇的问题
    EF中一对多的关系中,用单字段保存ID拼接字符串
    美行Thinkpad八通道快捷入口
    使用sqlserver的游标功能来导数据的常见写法
    JMeter教程01-下载和安装
    Windows无法安装到GPT分区形式磁盘的解决办法
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5972123.html
Copyright © 2011-2022 走看看