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

    定义

    桥接模式,指将抽象部分与具体实现部分分离,使它们都可以独立地变化,属于结构型设计模式。

    桥接模式的主要的目的是通过组合的方式建立两个类之间的联系,而不是继承,桥接模式是比多层继承更好的替代方案,桥接模式的核心在于把抽象与实现解耦。

    使用场景:

    • 在抽象和具体实现之间需要增加更多灵活性的场景
    • 一个类存在两个(或多个)独立变化的维度,而这两个(或多个)维度都需要独立进行扩展
    • 不希望使用继承,或因为多层继承导致系统类数目的激增。

    通用写法

    //抽象
    public abstract class Abstraction {
    
        protected IImplementor iImplementor;
    
        public Abstraction(IImplementor iImplementor) {
            this.iImplementor = iImplementor;
        }
    
        public void operationImpl(){
            this.iImplementor.operationImpl();
        }
    }
    
    //抽象实现
    public interface IImplementor {
        void operationImpl();
    }
    
    public class RefinedAbstraction extends Abstraction{
        public RefinedAbstraction(IImplementor iImplementor) {
            super(iImplementor);
        }
    
        @Override
        public void operationImpl() {
            super.operationImpl();
            System.out.println("RefinedAbstraction operationImpl ...");
        }
    }
    
    //具体实现
    public class ConcreteImplementor implements IImplementor{
        @Override
        public void operationImpl() {
            System.out.println("ConcreteImplementor operationImpl ...");
        }
    }
    //具体实现
    public class ConcreteImplementor2 implements IImplementor {
        @Override
        public void operationImpl() {
            System.out.println("ConcreteImplementor2 operationImpl ...");
        }
    }
    

    测试:

        public static void main(String[] args) {
            Abstraction abstraction = new RefinedAbstraction(new ConcreteImplementor());
            abstraction.operationImpl();
        }
    

    uml类图:

    image-20210102165152718

    举例

    image-20210102170609397

    对于基本几何图形,有三角形,四边形等,每种集合图形有不同颜色(红色,黑色等),下面用桥接模式实现。

    这里只实现(三角形和红色)

    public interface Color {
        public void bepaint(String shape);
    }
    public class Red implements Color {
        @Override
        public void bepaint(String shape) {
            System.out.println("红色的"+shape);
        }
    }
    
    public abstract class Shape {
        public Color color;
    
        public Shape(Color color) {
            this.color = color;
        }
    
        public abstract void draw();
    }
    public class Triangle extends Shape{
        public Triangle(Color color) {
            super(color);
        }
    
        @Override
        public void draw() {
            color.bepaint("三角形");
        }
    }
    

    测试:

            Shape triangle = new Triangle(new Red());
            triangle.draw();
    

    优缺点

    优点

    • 1、分离抽象接口及其实现部分。提高了比继承更好的解决方案。
    • 2、桥接模式提高了系统的可扩充性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统。
    • 3、实现细节对客户透明,可以对用户隐藏实现细节。

    缺点

    • 1、桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。
    • 2、桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围具有一定的局限性。
  • 相关阅读:
    Maven--反应堆(Reactor)
    Maven--超级 POM
    Maven--插件管理
    解决非模态对话框第二次创建失败问题
    【转】VerQueryValue失败的解决办法
    【转】SYSTEM_HANDLE_INFORMATION
    安全版字符串操作函数
    int转string的3种方法
    PE格式详细讲解3
    PE格式详细讲解2
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14223306.html
Copyright © 2011-2022 走看看