zoukankan      html  css  js  c++  java
  • 漫谈设计模式--3分钟理解桥接模式:笔和画的关系

    其实不需要3分钟,3秒钟就够了,记住桥接模式就是如此简单:一句话,笔有千般形,画有万变化。

    下面的仅仅助于理解。

    1. 定义

    The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently"

    From Wikipedia, the free encyclopedia:http://en.wikipedia.org/wiki/Bridge_pattern

    2. 应用场景

       The bridge pattern is useful when both the class as well as what it does vary often

       例如用笔作画,笔有铅笔、钢笔、毛笔、水笔、电脑等;图形有:直线、圆、三角形、树叶等等。各自独立变化。

    3. 结构

    Bridge UML class diagram.svg

    Abstraction (abstract class)
    defines the abstract interface
    maintains the Implementor reference.
    RefinedAbstraction (normal class)
    extends the interface defined by Abstraction
    Implementor (interface)
    defines the interface for implementation classes
    ConcreteImplementor (normal class)
    implements the Implementor interface

    3. 实例

    The following Java (SE 6) program illustrates the 'shape' example given below.

    简单描述就是笔和图形的关系:

    笔有铅笔、钢笔、毛笔、水笔等等;图形有:直线、圆、三角形、树叶等等。各自独立变化。

    /** "Implementor" */
    interface DrawingAPI {
        public void drawCircle(double x, double y, double radius);
    }
     
    /** "ConcreteImplementor"  1/2 */
    class DrawingAPI1 implements DrawingAPI {
        public void drawCircle(double x, double y, double radius) {
            System.out.printf("API1.circle at %f:%f radius %f
    ", x, y, radius);
        }
    }
     
    /** "ConcreteImplementor" 2/2 */
    class DrawingAPI2 implements DrawingAPI {
        public void drawCircle(double x, double y, double radius) {
            System.out.printf("API2.circle at %f:%f radius %f
    ", x, y, radius);
        }
    }
     
    /** "Abstraction" */
    abstract class Shape {
        protected DrawingAPI drawingAPI;
     
        protected Shape(DrawingAPI drawingAPI){
            this.drawingAPI = drawingAPI;
        }
     
        public abstract void draw();                             // low-level
        public abstract void resizeByPercentage(double pct);     // high-level
    }
     
    /** "Refined Abstraction" */
    class CircleShape extends Shape {
        private double x, y, radius;
        public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
            super(drawingAPI);
            this.x = x;  this.y = y;  this.radius = radius;
        }
     
        // low-level i.e. Implementation specific
        public void draw() {
            drawingAPI.drawCircle(x, y, radius);
        }
        // high-level i.e. Abstraction specific
        public void resizeByPercentage(double pct) {
            radius *= pct;
        }
    }
     
    /** "Client" */
    class BridgePattern {
        public static void main(String[] args) {
            Shape[] shapes = new Shape[] {
                new CircleShape(1, 2, 3, new DrawingAPI1()),
                new CircleShape(5, 7, 11, new DrawingAPI2()),
            };
     
            for (Shape shape : shapes) {
                shape.resizeByPercentage(2.5);
                shape.draw();
            }
        }
    }

    It will output:

    API1.circle at 1.000000:2.000000 radius 7.5000000
    API2.circle at 5.000000:7.000000 radius 27.500000

     无聊加入下面的文字,凑够博客园的文字篇幅要求,不用看。

    Bridge模式的概念

    Bridge 模式是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。

    Bridge模式的应用场景

    面向对象的程序设计(OOP)里有类继承(子类继承父类)的概念,如果一个类或接口有多个具体实现子类,如果这些子类具有以下特性:
    - 存在相对并列的子类属性。
    - 存在概念上的交叉。
    - 可变性。
    我们就可以用Bridge模式来对其进行抽象与具体,对相关类进行重构。

  • 相关阅读:
    shell脚本中echo颜色设置
    整合Spring+Hibernate+Struts2的时候发现json数据一直无法传到页面,提示no-Session
    分页查询——Hibernate Criteria实现一次查询取得总记录数和分页后结果集
    JS,JQ 格式化小数位数
    简单地做一下“回到顶部”按钮,用jQuery实现其隐藏和显示
    二级联动,三级联动,初学者,纯javascript,不含jQuery
    Oracle数据库知识要点
    ParameterizedType理解笔记
    JDBC mysql 相关内容笔记
    在做关于NIO TCP编程小案例时遇到无法监听write的问题,没想到只是我的if语句的位置放错了位置,哎,看了半天没看出来
  • 原文地址:https://www.cnblogs.com/davidwang456/p/4025926.html
Copyright © 2011-2022 走看看