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)”

  • 相关阅读:
    [转] EJB 3和Spring技术体系比较
    JBOSS只能本机localhost和127.0.0.1能访问的解决
    JBOSS EAP 6.0+ Standalone模式安装成Windows服务
    IBM WebSphere MQ 7.5基本用法
    maven学习(上)- 基本入门用法
    mac下环境变量、maven3.1.1 及 jdk1.7.0.45配置
    java:读/写配置文件
    java:使用匿名类直接new接口
    java与c#的反射性能比较
    XmlSpy / XSD 以及 验证
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5972123.html
Copyright © 2011-2022 走看看