• 桥接设计模式(Bridge)


    Bridge???

    Bridge的意思是“桥梁”。就像在现实世界中,桥梁的功能是将河流的两侧连接起来一样,Bridge模式的作用也是将两样东西连接起来,它们分别是类的功能层次结构和类的实现层次结构。

    • 父类通过声明抽象方法来定义接口(APl):需要使用继承强关联方式,那么必须使用中间层抽象子类来实现方法的可变性。所以在聚合关系中考虑引入DisplayImpl 抽象层
    • 子类通过实现具体方法来实现接口(APl):这种层次结构被称为“类的实现层次结构”。当我们以其他方式实现AbstractClass时,例如要实现一个AnotherConcreteClass时,类的层次结构会稍微发生一些变化。

    理解职责

    通俗理解办法:Bridge 是连通桥梁两端 是具体抽象层 与具体的实现类 建立关系的必然。

    需要明确的:序中依赖于操作系统的部分划分为Windows版、Macintosh版、Unix版,那么我们就可以用Bridge模式中的“类的实现层次结构”来表现这些依赖于操作系统的部分。也就是说,我们需要编写一个定义这些操作系统的共同接口(API)的Implementor角色,然后编写Windows版、Macintosh版、Unix版的3个Concretelmplementor角色。这样一来,无论在“类的功能层次结构”中增加多少个功能,它们都可以工作于这3个操作系统上。

    职责实现功能:完成不同功能的字符显示

    在桥的哪一侧|名字|说明
    |类的功能层次结构 ====>|Display|负责“显示”的类
    类的功能层次结构 ====>|CountDisplay|增加了“只显示规定次数”这一功能的类
    类的实现层次结构 ====>|DisplayImpl 负责“显示”的类
    |类的实现层次结构 ====>|stringDisplayImp1“用字符串显示”的类
    Main测试程序行为 ====>的类

    UML

    Code

    Display:

    
    public class Display {
    
        /**
         * 交给抽象子类去实现
         */
        private DisplayImpl displayimpl;
    
        public Display(DisplayImpl displayimpl) {
            this.displayimpl = displayimpl;
        }
    
        protected void open(){
              displayimpl.rawOpen();
        }
    
        protected void printr(){
             displayimpl.rawPrint();
        }
    
        protected void close(){
            displayimpl.rawClose();
        }
    
        public void display(){
            open();
            printr();
            close();
        }
    }
    
    
    

    假设需求改变 我们需要在Display基础上增加功能:

    
    public class randomDisplay extends Display {
    
        public randomDisplay(DisplayImpl displayimpl) {
            super(displayimpl);
        }
    
    
        public void randomDisplay(int times) {
            Random random = new Random();
            int len=random.nextInt(times);
            super.open();
            for (int i = 0; i < len; i++) {
                super.printr();
            }
            super.close();
        }
    }
    
    public class CountDisplay extends Display {
    
        public CountDisplay(DisplayImpl displayimpl) {
            super(displayimpl);
        }
    
        public void multiDisplay(int times){
            super.open();
            for (int i = 0; i < times; i++) {
                super.printr();
            }
            super.close();
        }
    }
    
    
    

    书上没有明确这个 称之为 抽象实现者 :我暂且称之为 Bridge器 必须抽象

    
    public abstract class DisplayImpl {
    
        /**
         *  作为bridge 强关联 解耦的关键性 地方 不可以为 class 必须为abstract class
         */
        public abstract void rawOpen();
    
        public abstract void rawPrint();
    
        public abstract void rawClose();
    }
    
    

    测试:

    
     **/
    public class MainT {
    
    
        public static void main(String[] args) {
    
           Display display=new Display(new StringDisplayImpl("bridge design method"));
    
           Display display1=new CountDisplay(new StringDisplayImpl("on first bridge"));
    
           display.display();
    
           display1.display();
    
           ((CountDisplay) display1).multiDisplay(3);
    
           // 随机次数
            Display display2=new randomDisplay(new StringDisplayImpl("two sencond bridge"));
    
            ((randomDisplay) display2).randomDisplay(5);
    
        }
    }
    
    
  • 相关阅读:
    .dll 无法查找或者打开PDB文件
    VC++中解决“在查找预编译头使用时跳过”的方法
    如何重置设置开发环境
    opencv与VS的配置
    supermap开发webgis的经验
    Json 与GeoJson
    地理配准
    DBMS
    C#三层构架
    重装系统简要步骤
  • 原文地址:https://www.cnblogs.com/dgwblog/p/9781199.html
Copyright © 2020-2023  润新知