zoukankan      html  css  js  c++  java
  • 桥接模式

    描述

    桥接模式是将抽象化与实现化分离,即将对象的定义和实现进行分离,是结构模式的一种。这种类涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。

    例子

    比如我们要画图,我们要画的可能有正方形、矩形、三角形等等,还要给这些形状上色,分别为红色、黑色、蓝色等等。

    一种复杂的方案是为每种形状都准备所有可能的颜色,然后根据需求选择:

    为了降低空间复杂度和时间复杂度,我们提供两个父类:形状父类和颜色父类,两个父类包含相关子类,然后根据需求对形状和颜色组合:

    模式结构

    UML图如下:

    桥接模式主要包含如下几个角色:

          Abstraction:抽象类。 
          RefinedAbstraction:扩充抽象类。 
          Implementor:实现类接口。 
          ConcreteImplementor:具体实现类 。

    实例

    我们实现一个实例,UML图如下:

    首先是形状类 Shape.java

    public abstract class Shape {
        Color color;
    
        public void setColor(Color color) {
            this.color = color;
        }
        
        public abstract void draw();
    }
    

    然后实现三个形状子类:圆形、长方形、正方形。

    public class Circle extends Shape{
    
        public void draw() {
            color.bepaint("正方形");
        }
    }
    
    public class Rectangle extends Shape{
    
        public void draw() {
            color.bepaint("长方形");
        }
    }
    
    public class Square extends Shape{
    
        public void draw() {
            color.bepaint("正方形");
        }
    }
    

    然后是颜色接口:Color.java

    public interface Color {
        public void bepaint(String shape);
    }
    

    实现颜色接口,实现三种颜色类型:白色、灰色、黑色

    public class White implements Color{
    
        public void bepaint(String shape) {
            System.out.println("白色的" + shape);
        }
    }
    
    public class Gray implements Color{
    
        public void bepaint(String shape) {
            System.out.println("灰色的" + shape);
        }
    }  
    public class Black implements Color{
    
        public void bepaint(String shape) {
            System.out.println("黑色的" + shape);
        }
    }
    

    客户端:

    public class Client {
        public static void main(String[] args) {
            //白色
            Color white = new White();
            //正方形
            Shape square = new Square();
            //白色的正方形
            square.setColor(white);
            square.draw();
            
            //长方形
            Shape rectange = new Rectangle();
            rectange.setColor(white);
            rectange.draw();
        }
    }
    

    W3school的实例代码:

    interface Printer {
       public void print(int radius, int x, int y);
    }
    class ColorPrinter implements Printer {
       @Override
       public void print(int radius, int x, int y) {
          System.out.println("Color: " + radius +", x: " +x+", "+ y +"]");
       }
    }
    class BlackPrinter implements Printer {
       @Override
       public void print(int radius, int x, int y) {
          System.out.println("Black: " + radius +", x: " +x+", "+ y +"]");
       }
    }
    abstract class Shape {
       protected Printer print;
       protected Shape(Printer p){
          this.print = p;
       }
       public abstract void draw();  
    }
    class Circle extends Shape {
       private int x, y, radius;
    
       public Circle(int x, int y, int radius, Printer draw) {
          super(draw);
          this.x = x;  
          this.y = y;  
          this.radius = radius;
       }
    
       public void draw() {
          print.print(radius,x,y);
       }
    }
    public class Main {
       public static void main(String[] args) {
          Shape redCircle = new Circle(100,100, 10, new ColorPrinter());
          Shape blackCircle = new Circle(100,100, 10, new BlackPrinter());
    
          redCircle.draw();
          blackCircle.draw();
       }
    }
    

    使用场景

    1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。

    2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。

    3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展。

    总结

    1、桥接模式实现了抽象化与实现化的脱耦。他们两个互相独立,不会影响到对方。

    2、对于两个独立变化的维度,使用桥接模式再适合不过了。

    3、对于“具体的抽象类”所做的改变,是不会影响到客户。

    特别感谢:https://www.w3cschool.cn/java/java-bridge-pattern.htmlhttps://www.cnblogs.com/chenssy/p/3317866.html

        

  • 相关阅读:
    Python 字符串操作
    Python 字典操作
    16 飞机大战:思路整理、重要代码
    15 飞机大战:pygame入门、python基础串连
    14 windows下安装pygame模块
    13 制作模块压缩包、安装模块
    12 包及导包
    11 模块、模块的搜索顺序、__file__内置属性、__name__属性
    异常集
    10 异常
  • 原文地址:https://www.cnblogs.com/K-artorias/p/7884142.html
Copyright © 2011-2022 走看看