0
点赞
收藏
分享

微信扫一扫

【精品】关于枚举的高级用法

君心浅语 2024-02-18 阅读 12

文章目录

第八章_桥接模式

1.介绍

1.1定义

1.2使用场景

1.2.1生活场景

1.2.2java场景

1.3角色

2.举例

2.1生活举例

// 抽象类 TerrainType:行驶地形
abstract class TerrainType {
    //行驶
    abstract void setTerrain();
}

// 具体类 OnRoadTerrain
class OnRoadTerrain extends TerrainType {
    @Override
    void setTerrain() {
        System.out.println("在公路上行驶");
    }
}

// 具体类 OffRoadTerrain
class OffRoadTerrain extends TerrainType {
    @Override
    void setTerrain() {
        System.out.println("在草地上行驶");
    }
}

// 具体类 BeachTerrain
class BeachTerrain extends TerrainType {
    @Override
    void setTerrain() {
        System.out.println("在沙滩上行驶");
    }
}
// 接口 TireType:轮胎类型
interface TireType {
    void setTire();
}

// 具体类 StandardTire
class StandardTire implements TireType {
    @Override
    public void setTire() {
        System.out.println("使用普通轮胎");
    }
}

// 具体类 OffRoadTire
class OffRoadTire implements TireType {
    @Override
    public void setTire() {
        System.out.println("使用越野轮胎");
    }
}

// 具体类 SnowTire
class SnowTire implements TireType {
    @Override
    public void setTire() {
        System.out.println("使用雪地轮胎");
    }
}
// 桥接类 RemoteControlCar
class RemoteControlCar {
    private TerrainType terrain;	//行驶地形抽象类
    private TireType tire;			//轮胎类型接口

    public RemoteControlCar(TerrainType terrain, TireType tire) {
        this.terrain = terrain;
        this.tire = tire;
    }

    public void drive() {
        this.terrain.setTerrain();	
    }

    public void changeTire() {
        this.tire.setTire();
    }
}
public class BridgePatternExample {
    public static void main(String[] args) {
        TerrainType onRoad = new OnRoadTerrain();
        TerrainType offRoad = new OffRoadTerrain();
        TerrainType beach = new BeachTerrain();

        TireType standard = new StandardTire();
        TireType offRoadTire = new OffRoadTire();
        TireType snowTire = new SnowTire();

        RemoteControlCar car1 = new RemoteControlCar(onRoad, standard);
        RemoteControlCar car2 = new RemoteControlCar(offRoad, offRoadTire);
        RemoteControlCar car3 = new RemoteControlCar(beach, snowTire);

        car1.drive();
        car1.changeTire();

        car2.drive();
        car2.changeTire();

        car3.drive();
        car3.changeTire();
    }
}

2.2JDK源码举例

image-20240213203603440

2.2.1总览

public class JDBCTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            //STEP 1: Register JDBC driver	//注册mysql的Driver,即com.mysql.jdbc.Driver类就会被加载,注册到registeredDrivers这个特殊的list中
            Class.forName("com.mysql.jdbc.Driver");
            //STEP 2: Open a connection		//获取mysql规范的连接,从registeredDrivers中来获取已经注册的driver
            conn = DriverManager.getConnection("jdbc:mysql://192.168.108.145/test", "root", "root");
            //STEP 3: Execute a query
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userinfo limit 1");
            //STEP 4: Get results
            while(rs.next()){
                System.out.println(rs.getString("id"));
            }
            rs.close();
        }catch(SQLException se){
            ......
        }//end try
    }
}

2.2.2Driver

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    // Register ourselves with the DriverManager
    static {
        try {
            //进行了驱动的注册
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

    //Construct a new driver and register it with DriverManager
    public Driver() throws SQLException {
        // Required for Class.forName().newInstance()
    }
}

2.2.3DriverManager

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

3.优缺点

4.桥接模式与适配器模式

4.1桥接模式和适配器模式的区别和联系

image-20240213211044773

image-20240213211127224

4.2适配器与桥接模式的联合

image-20240213211357290

image-20240213211521225

image-20240213211538847

举报

相关推荐

0 条评论