一、手机操作问题

 
对不同类型、不同品牌的手机实现操作编程(比如:开机、关机、打电话等),如图:
在这里插入图片描述
 

二、传统方案解决手机操作问题

 

  • 类图:
    在这里插入图片描述
    传统方案解决手机操作问题分析:
    (1)扩展性问题(类爆炸):如果再增加手机的样式(旋转式),则需要增加各个品牌的手机类到该样式下。同样如果增加一个手机的品牌,则需要在各个手机样式类下增加该品牌。
    (2)违反了单一职责原则,当增加手机样式时,同时也要增加所有品牌的手机,这样增加了代码维护成本。
    (3)解决方案:使用桥接模式。
     

三、桥接模式介绍

 

1、基本介绍

 
桥接模式(Bridge模式):将实现与抽象放在两个不同的类层次中,使两个层次可以独立改变。

桥接模式是一种结构型设计模式。

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

2、原理类图

 
在这里插入图片描述
原理类图说明:
(1)Client类:桥接模式的调用者。
(2)抽象类(Abstraction):维护了Implementor接口(即它的实现类ConcreteImplementorA和ConcreteImplementorB),二者是聚合关系,Abstraction充当桥接类。
(3)RefinedAbstraction :RefinedAbstraction是 Abstraction 抽象类的子类。
(4)Implementor:行为实现类的接口。
(5)ConcreteImplementorA /B :行为的具体实现类。
(6)从UML图中可以看出:这里的抽象类和接口是聚合的关系,其实调用和被调用关系。
 

四、桥接模式解决手机操作问题

 
使用桥接模式改进传统方式,让程序具有较好的扩展性,有利于程序维护。

  • 类图:
    在这里插入图片描述
  • 实现代码:
package com.etc.design.bridge;

public abstract class Phone {
	// 品牌(组合关系)
	private Brand brand;
	// 构造器
	public Phone(Brand brand) {
		super();
		this.brand = brand;
	}
	protected void open() {
		this.brand.open();
	}
	protected void call() {
		brand.call();
	}
	protected void close() {
		brand.close();
	}
}
package com.etc.design.bridge;

// 折叠式手机类,继承抽象类Phone
public class FoldedPhone extends Phone {
	// 构造器
	public FoldedPhone(Brand brand) {
		super(brand);
	}
	public void open() {
		super.open();
		System.out.println(" 折叠样式手机 ");
	}
	public void call() {
		super.call();
		System.out.println(" 折叠样式手机 ");
	}
	public void close() {
		super.close();
		System.out.println(" 折叠样式手机 ");
	}
}
package com.etc.design.bridge;

// 直立式手机类,继承抽象类Phone
public class UpRightPhone extends Phone {
	// 构造器
	public UpRightPhone(Brand brand) {
		super(brand);
	}
	public void open() {
		super.open();
		System.out.println(" 直立样式手机 ");
	}
	public void call() {
		super.call();
		System.out.println(" 直立样式手机 ");
	}
	public void close() {
		super.close();
		System.out.println(" 直立样式手机 ");
	}
}
package com.etc.design.bridge;

// 手机品牌接口
public interface Brand {
	// 开机方法
	void open();
	// 打电话方法
	void call();
	// 关机方法
	void close();
}
package com.etc.design.bridge;

public class Vivo implements Brand {
	@Override
	public void open() {
		System.out.println("Vivo手机-开机");
	}
	@Override
	public void call() {
		System.out.println("Vivo手机-打电话");
	}
	@Override
	public void close() {
		System.out.println("Vivo手机-关机");
	}
}
package com.etc.design.bridge;

public class XiaoMi implements Brand {
	@Override
	public void open() {
		System.out.println("小米手机-开机");
	}
	@Override
	public void call() {
		System.out.println("小米手机-打电话");
	}
	@Override
	public void close() {
		System.out.println("小米手机-关机");
	}
}
package com.etc.design.bridge;

public class Client {
	public static void main(String[] args) {
		System.out.println("=======折叠式手机-小米=======");
		// 1、折叠式手机-小米
		Phone phone1 = new FoldedPhone(new XiaoMi());
		phone1.open();
		phone1.call();
		phone1.close();
		System.out.println("=======直立式手机-Vivo=======");
		// 2、直立式手机-Vivo
		Phone phone2 = new FoldedPhone(new Vivo());
		phone2.open();
		phone2.call();
		phone2.close();
		System.out.println("=======旋转式手机-小米=======");
		// 3、旋转式手机-小米
		UpRightPhone phone3 = new UpRightPhone(new XiaoMi());
		phone3.open();
		phone3.call();
		phone3.close();
		System.out.println("=======旋转式手机-Vivo=======");
		// 4、旋转式手机-Vivo
		UpRightPhone phone4 = new UpRightPhone(new Vivo());
		phone4.open();
		phone4.call();
		phone4.close();
	}
}

 

五、桥接模式在JDBC的源码剖析

 
桥接模式在JDBC的源码剖析:Jdbc的Driver接口,如果从桥接模式来看,Driver就是一个接口,下面可以有MySQL的Driver、Oracle的Driver,这些就可以当做实现接口类。

  • 类图:
    在这里插入图片描述

(1)数据库连接-步骤1:注册驱动。

package com.mysql.jdbc;
...
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    static {
        try {
        	
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }
	...
}

(2)数据库连接-步骤2:获取数据库连接。

调用DriverManager中的getConnection方法,创建与数据库的连接,并返回连接对象(Connection类的实例)。

DriverManager类中getConnection方法一共有四个(重载方法),前三个由public修饰,用来获取不同类型的参数,这三个getConnection方法实际相当于一个入口,最终它们都会调用第四个private修饰的getConnection方法。每个getConnection方法中参数都含有URL,该URL指向数据库的地址。对于前三个由public修饰的方法,第一种和第二种方法都是将账号和密码放在Properties中,而第三种方法需要将用户名和密码携带在URL中。

(1)Connection getConnection(String url,java.util.Properties info)
(2)Connection getConnection(String url,String user, String password) 
(3)Connection getConnection(String url)
(4)Connection getConnection(String url, java.util.Properties info, Class<?> caller)

在这里插入图片描述

package java.sql;
...
public class DriverManager {
    ......
    @CallerSensitive
    public static Connection getConnection(String url,
        java.util.Properties info) throws SQLException {

        return (getConnection(url, info, Reflection.getCallerClass()));
    }
    ......
    @CallerSensitive
    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()));
    }
    ......
    @CallerSensitive
    public static Connection getConnection(String url)
        throws SQLException {

        java.util.Properties info = new java.util.Properties();
        return (getConnection(url, info, Reflection.getCallerClass()));
    }
    ......
    public static synchronized void registerDriver(java.sql.Driver driver)
        throws SQLException {

        registerDriver(driver, null);
    }
    ......
    public static synchronized void registerDriver(java.sql.Driver driver,
            DriverAction da)
        throws SQLException {

        /* Register the driver if it has not already been added to our list */
        if(driver != null) {
            registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
        } else {
            // This is for compatibility with the original DriverManager
            throw new NullPointerException();
        }

        println("registerDriver: " + driver);

    }
    ......
    public static synchronized void registerDriver(java.sql.Driver driver,
            DriverAction da)
        throws SQLException {

        /* Register the driver if it has not already been added to our list */
        if(driver != null) {
            registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
        } else {
            // This is for compatibility with the original DriverManager
            throw new NullPointerException();
        }

        println("registerDriver: " + driver);

    }
    ......
    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");
    }
    ......
}

在这里插入图片描述

package com.mysql.jdbc;
......
public class ConnectionImpl extends ConnectionPropertiesImpl implements MySQLConnection {
    ......
}

 

六、桥接模式的注意事项和细节

 
(1)实现了抽象和实现部分的分离,从而极大的提供了系统的灵活性,让抽象部分和实现部分独立开来,这有助于系统进行分层设计,从而产生更好的结构化系统。
(2)对于系统的高层部分,只需要知道抽象部分和实现部分的接口就可以了,其它的部分由具体业务来完成。
(3)桥接模式替代多层继承方案,可以减少子类的个数,降低系统的管理和维护成本。
(4)桥接模式的引入增加了系统的理解和设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计和编程。
(5)桥接模式要求正确识别出系统中两个独立变化的维度(抽象和实现),因此其使用范围有一定的局限性,即需要有这样的应用场景。

 

七、桥接模式应用场景

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

常见的应用场景:

(1)JDBC驱动程序
(2)银行转账系统

转账分类(抽象): 网上转账、柜台转账、AMT 转账。
转账用户类型(实现):普通用户、银卡用户、金卡用户。

(3)消息管理

消息类型(抽象):即时消息、延时消息。
消息分类(实现):手机短信、邮件消息、QQ 消息。

更多推荐