1.桥接模式测试
package com.zhaoshuangjian.mode10_桥接模式; import com.zhaoshuangjian.mode10_桥接模式.mode10.database.Driver; import com.zhaoshuangjian.mode10_桥接模式.mode10.database.DriverManager; import com.zhaoshuangjian.mode10_桥接模式.mode10.database.MySqlDriver; import com.zhaoshuangjian.mode10_桥接模式.mode10.database.OracleDriver; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color.Color; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color.Green; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color.Red; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color.White; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape.Circle; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape.Rectangle; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape.Square; /** * <p>桥接模式测试</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class BridgeTest { public static void main(String[] args) { driverConnect(); drawShape(); /** * 1、分离抽象接口及其实现部分。提高了比继承更好的解决方案 * 2、桥接模式提高了系统的可扩充性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统 * 3、对客户隐藏实现细节 */ } /** * 不同驱动连接数据库 */ private static void driverConnect(){ DriverManager driverManager = new DriverManager(); String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true"; String user = "root"; String pwd = "root"; // mysql驱动连接数据库 Driver mySqlDriver = new MySqlDriver(url,user,pwd); driverManager.setDriver(mySqlDriver); driverManager.connect(); System.out.println("========= 分割线 ========="); // oracle驱动连接数据库 Driver oracleDriver = new OracleDriver(); driverManager.setDriver(oracleDriver); driverManager.connect(); System.out.println("\n========= 分割线 =========\n"); } /** * 绘制不同颜色的形状 */ private static void drawShape(){ //绘制红色的矩形 Color red = new Red(); Rectangle rectangle = new Rectangle(); rectangle.setColor(red); rectangle.draw(); //绘制绿色的正方形 Color green = new Green(); Square square = new Square(); square.setColor(green); square.draw(); //绘制白色的圆形 Color white = new White(); Circle circle = new Circle(); circle.setColor(white); circle.draw(); } }
2.各驱动之间的桥梁
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>各驱动之间的桥梁</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public abstract class AbstractBridge { // 聚合和关联驱动接口实例 private Driver driver; public Driver getDriver() { return driver; } public void setDriver(Driver driver) { this.driver = driver; } public abstract void connect(); }
3.数据库驱动接口
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>数据库驱动接口</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public interface Driver { /** * 连接 */ void connect(); }
4.驱动管理器 == 实现不同驱动设备之间的数据库连接
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>驱动管理器 == 实现不同驱动设备之间的数据库连接</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class DriverManager extends AbstractBridge { @Override public void connect() { getDriver().connect(); } }
5.MySql数据库驱动类
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>MySql数据库驱动类</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class MySqlDriver implements Driver { private String url; private String user; private String pwd; public MySqlDriver(String url, String user, String pwd){ this.url = url; this.user = user; this.pwd = pwd; } @Override public void connect() { System.out.println("url:"+this.url); System.out.println("user:"+this.user); System.out.println("pwd:"+this.pwd); System.out.println("mysql驱动连接数据库"); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
6.Oracle数据库驱动类
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>Oracle数据库驱动类</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class OracleDriver implements Driver { @Override public void connect() { System.out.println("oracle驱动连接数据库"); } }
7.PostGreSql数据库驱动类
package com.zhaoshuangjian.mode10_桥接模式.mode10.database; /** * <p>PostGreSql数据库驱动类</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class PostgreSqlDriver implements Driver { @Override public void connect() { System.out.println("postgresql驱动连接数据库"); } }
8.颜色接口
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color; /** * <p>颜色接口</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public interface Color { void painting(String shape); }
9.绿色
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color; /** * <p>绿色</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class Green implements Color{ @Override public void painting(String shape) { System.out.println("绘制绿色的"+shape); } }
10.红色
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color; /** * <p>红色</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class Red implements Color{ @Override public void painting(String shape) { System.out.println("绘制红色的"+shape); } }
11.白色
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color; /** * <p>白色</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class White implements Color{ @Override public void painting(String shape) { System.out.println("绘制白色的"+shape); } }
12.抽象形状类 – 聚合与关联Color实现类
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape; import com.zhaoshuangjian.mode10_桥接模式.mode10.paint.color.Color; /** * <p>抽象形状类 -- 聚合与关联Color实现类</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public abstract class AbstractShape { private Color color; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } /** * 抽象方法 == 绘制图案 */ abstract void draw(); }
13.圆形
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape; /** * <p>圆形</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class Circle extends AbstractShape { @Override public void draw() { getColor().painting("圆形"); } }
14.矩形、长方形
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape; /** * <p>矩形、长方形</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class Rectangle extends AbstractShape { @Override public void draw() { getColor().painting("矩形"); } }
15.正方形
package com.zhaoshuangjian.mode10_桥接模式.mode10.paint.shape; /** * <p>正方形</p> * * @Author zhaoshuangjian 2023-03-25 下午20:39 */ public class Square extends AbstractShape { @Override public void draw() { getColor().painting("正方形"); } }