关于设计模式:设计模式之桥接模式

7次阅读

共计 5298 个字符,预计需要花费 14 分钟才能阅读完成。

本文通过老王和小王买车,引出设计模式中的结构型设计之桥接模式,接着阐明设计型模式的概念和代码实现,为了加深了解,会阐明适配器设计模式在 JDBC 中的利用,最初谈谈桥接模式和适配器模式的总结。

读者能够拉取残缺代码到本地进行学习,实现代码均测试通过后上传到码云。

一、引出问题

老王和小王去飞驰 4S 店买车,飞驰 4S 店的各种品牌型号目不暇接,老王想试驾飞驰 E、小王想试驾飞驰 G,并且提出两种飞驰型号的各种色彩都想体验一把,这让店小二犯了难,两两组合就是很多种,4S 店压根放不下。

无奈店小二求救经理,经理出了一个留神:将飞驰 E 和 G 开的品牌形象进去,将色彩也形象进去,通过品牌和色彩的组合代替继承关系,缩小了色彩和品牌的耦合,且缩小了车的个数,只须要两台就够了。

果然经理不愧是经理。

经理所说的其实就是桥接模式。这种模式波及到一个作为桥接的接口,使得实体类的性能独立于接口实现类。这两种类型的类可被结构化扭转而互不影响。

二、概念与应用

咱们看一些概念:桥接(Bridge)是用于把抽象化与实现化解耦,使得二者能够独立变动。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接构造,来实现二者的解耦。

在该模式中应该波及到四个角色:

①实现类接口(Implementor):定义实现角色的接口,供扩大抽象化角色应用,例如形象出飞驰品牌 benz 能够扩大出 benzE benzG

②具体实现角色(ConcreteImplementor):实现类的具体实现,例如各种飞驰品牌

③抽象化(Abstraction)角色:定义一个抽象类,其中援用了实现化角色(想要组合),例如汽车产品

④扩大抽象化(RefinedAbstraction)角色:抽象化角色子类,实现父类办法,且通过组合关系调用实现化角色中的业务办法,例如具体飞驰产品,红色飞驰、红色飞驰

依据该模式的定义,咱们将飞驰品牌形象进去,而后各品牌有各自的实现,每个色彩的车把车品牌组合进来,在客户端中每个相机类型和相机品牌都能两两组合。

咱们看具体的代码实现:

实现类接口:

/**
 * 飞驰品牌类
 * @author tcy
 * @Date 05-08-2022
 */
public interface BenzBrand {void showInfo();
}

具体实现角色 1:

/**
 * @author tcy
 * @Date 05-08-2022
 */
public class BenzE implements BenzBrand{
    @Override
    public void showInfo() {System.out.print("【飞驰 E】色彩是:");
    }
}

具体实现角色 2:

/**
 * @author tcy
 * @Date 05-08-2022
 */
public class BenzG implements BenzBrand{
    @Override
    public void showInfo() {System.out.print("【飞驰 G】色彩是:");

    }
}

抽象化角色:

/**
 * 形象飞驰类
 * @author tcy
 * @Date 05-08-2022
 */
public abstract class Benz {
    // 将品牌组合进来
    protected BenzBrand benzBrand;

    public Benz(BenzBrand benzBrand) {this.benzBrand = benzBrand;}

    public void showInfo(){benzBrand.showInfo();
    }


}

扩大抽象化 1:

/**
 * @author tcy
 * @Date 05-08-2022
 */
public class BlackBenz extends Benz {public BlackBenz(BenzBrand benzBrand) {super(benzBrand);
    }

    @Override
    public void showInfo() {super.showInfo();

        System.out.println("彩色...");
    }


}

扩大抽象化 2:

/**
 * @author tcy
 * @Date 05-08-2022
 */
public class RedBenz extends Benz {public RedBenz(BenzBrand benzBrand) {super(benzBrand);
    }
    @Override
    public void showInfo() {super.showInfo();
        System.out.println("红色...");
    }

}

客户端调用:

/**
 * @author tcy
 * @Date 05-08-2022
 */
public class Client {public static void main(String[] args) {
        // 彩色飞驰 E
        Benz benz1 = new BlackBenz(new BenzE());
        benz1.showInfo();
        // 彩色飞驰 G
        Benz benz2 = new BlackBenz(new BenzG());
        benz2.showInfo();
        // 红色飞驰 E
        Benz benz3 = new RedBenz(new BenzE());
        benz3.showInfo();
        // 红色飞驰 G
        Benz benz4 = new RedBenz(new BenzG());
        benz4.showInfo();}
【飞驰 E】色彩是: 彩色...【飞驰 G】色彩是: 彩色...【飞驰 E】色彩是: 红色...【飞驰 G】色彩是: 红色...

这样即便老王提出来新的色彩、新的车型,只须要减少相应的具体实现角色或者扩大抽象化角色即可。

顾名思义,桥接模式就像是一个桥,能够用来连贯两个不同中央,这两个中央自在倒退,两头的贸易是通过一座桥来连贯。

这种办法的毛病也很显著,汽车能很快的确立型号和色彩两个维度,在理论业务开发中,辨认出零碎两个独立变动的维度就不简略了。

不难看出,列举的例子有些过于强求,在事实世界中是永远不可能产生的,为了加深了解我找了大量在 JDK 亦或是 Spirng 等各种框架对桥接模式的利用,只找到了桥接模式在 Jdbc 中的利用。

三、利用

咱们都晓得通过 JDBC 能够实现 Java 对关系型数据库的 SQL 操作,咱们在连贯数据数据库时,想必都接触过 Driver,在连贯 MySQL 和 Oracle 的 Driver 都是不同的,这些都是实现接口类。

咱们看一下 MySQL 中实现的 Driver 类。

public class Driver extends NonRegisteringDriver implements java.sql.Driver {public Driver() throws SQLException { }

    static {
        try {DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {throw new RuntimeException("Can't register driver!");
        }
    }
}

在该类中实际上有两个作用,一是调用了 DriverManager 中的 registerDriver 办法来注册驱动,二是当驱动注册实现后,咱们就会开始调用 DriverManager 中的 getConnection 办法了。

咱们看 DriverManager 的残缺代码:

    public class DriverManager {
    
    public static Connection getConnection(String url,
        String user, String password) throws SQLException {java.util.Properties info = new java.util.Properties();
    if (user != null) {info.put("user", user);
    }
    if (password != null) {info.put("password", password);
    }

    return (getConnection(url, info, Reflection.getCallerClass()));
}

private static Connection getConnection(String url, java.util.Properties info, Class<?> caller) throws SQLException {
    /*
     * When callerCl is null, we should check the application's
     * (which is invoking this class indirectly)
     * classloader, so that the JDBC driver class outside rt.jar
     * can be loaded from here.
     */
    ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
    synchronized(DriverManager.class) {
        // synchronize loading of the correct classloader.
        if (callerCL == null) {callerCL = Thread.currentThread().getContextClassLoader();}
    }

    if(url == null) {throw new SQLException("The url cannot be null", "08001");
    }

    println("DriverManager.getConnection(\"" + url + "\")");

    // Walk through the loaded registeredDrivers attempting to make a connection.
    // Remember the first exception that gets raised so we can reraise it.
    SQLException reason = null;

    for(DriverInfo aDriver : registeredDrivers) {
        // If the caller does not have permission to load the driver then
        // skip it.
        if(isDriverAllowed(aDriver.driver, callerCL)) {
            try {println("trying" + aDriver.driver.getClass().getName());
                Connection con = aDriver.driver.connect(url, info);
                if (con != null) {
                    // Success!
                    println("getConnection returning" + aDriver.driver.getClass().getName());
                    return (con);
                }
            } catch (SQLException ex) {if (reason == null) {reason = ex;}
            }

        } else {println("skipping:" + aDriver.getClass().getName());
        }

    }

    // if we got here nobody could connect.
    if (reason != null)    {println("getConnection failed:" + reason);
        throw reason;
    }

    println("getConnection: no suitable driver found for"+ url);
    throw new SQLException("No suitable driver found for"+ url, "08001");
}
}
}

在 Java 中通过 Connection 提供给各个数据库一样的操作接口,这里的 Connection 能够看作抽象类。

能够说咱们用来操作不同数据库的办法都是雷同的,不过 MySQL 有本人的 ConnectionImpl 类,同样 Oracle 也有对应的实现类。

这里 Driver 和 Connection 之间是通过 DriverManager 类进行桥接的,这种桥接模式和咱们下面能够清晰的看进去各个角色是不同的。

四、总结

桥接模式是很好了解的,置信认真看了实例的同学应该都能够看懂,但那并不代表你曾经把握了该设计模式。在咱们应用 JDBC 的时候,想必有很多同学并不能看进去这是桥接模式。

纸上得来终觉浅,有一部分例子是为了阐明桥接模式而“构想”进去的,各个角色都是清晰直观。看了这样的代码,能够学会桥接模式,然而到了理论中很可能还是不会用。

最好的办法就是给出实在我的项目里的例子。然而这个难度的确很大,一到了实在我的项目里,就会遇到很多细节问题,从而影响对模式的了解,而且实在我的项目都带有肯定的业务环境。

看懂并且学会了设计模式是一回事,在理论开发中择优抉择设计模式那是另外一回事,这不仅须要对各个设计模式了解到位,更多的是对业务的了解和代码理念的把控。

举荐读者,参考软件设计七大准则 认真浏览往期的文章,认真领会。

创立型设计模式

一、设计模式之工厂办法和形象工厂

二、设计模式之单例和原型

三、设计模式之建造者模式

结构型设计模式

四、设计模式之代理模式

五、设计模式之适配器模式

正文完
 0