ToB企服应用市场:ToB评测及商务社交产业平台

标题: 桥接模式 [打印本页]

作者: 王海鱼    时间: 2023-9-18 01:06
标题: 桥接模式
桥接模式

案例引入

对不同的手机类型和不同品牌实现操作(如开机,关机,上网,打电话等)。
如手机的类型有折叠式,直立式等。手机品牌有华为,小米,vivo等。
传统方式实现案例UML图


传统方式问题分析

基本介绍

桥接模式UML类图


类图说明

桥接模式实现案例

UML图


代码
  1. public interface Brand {
  2.     public void open();
  3.     public void close();
  4.     public void call();
  5. }
  6. public class Vivo implements Brand{
  7.     @Override
  8.     public void call() {
  9.         System.out.println("Vivo手机打电话");
  10.     }
  11.     @Override
  12.     public void open() {
  13.         System.out.println("Vivo手机开机");
  14.     }
  15.     @Override
  16.     public void close() {
  17.         System.out.println("Vivo手机关机");
  18.     }
  19. }
  20. public class XiaoMi implements Brand{
  21.     @Override
  22.     public void call() {
  23.         System.out.println("XiaoMi手机打电话");
  24.     }
  25.     @Override
  26.     public void open() {
  27.         System.out.println("XiaoMi手机开机");
  28.     }
  29.     @Override
  30.     public void close() {
  31.         System.out.println("XiaoMi手机关机");
  32.     }
  33. }
  34. public abstract class Phone {
  35.     private Brand brand;
  36.     public Phone(Brand brand){
  37.         this.brand = brand;
  38.     }
  39.     public Phone() {
  40.     }
  41.     public void open(){
  42.         brand.open();
  43.     }
  44.     public void close(){
  45.         brand.close();
  46.     }
  47.     public void call(){
  48.         brand.call();
  49.     }
  50. }
  51. public class FoldedPhone extends Phone{
  52.     public FoldedPhone(Brand brand){
  53.         super(brand);
  54.     }
  55.     public void open(){
  56.         super.open();
  57.         System.out.println("折叠样式手机");
  58.     }
  59.     public void close(){
  60.         super.close();
  61.         System.out.println("折叠样式手机");
  62.     }
  63.     public void call(){
  64.         super.call();
  65.         System.out.println("折叠样式手机");
  66.     }
  67. }
  68. public class UpRightPhone extends Phone{
  69.     public UpRightPhone(Brand brand){
  70.         super(brand);
  71.     }
  72.     public void open(){
  73.         super.open();
  74.         System.out.println("直立样式手机");
  75.     }
  76.     public void close(){
  77.         super.close();
  78.         System.out.println("直立样式手机");
  79.     }
  80.     public void call(){
  81.         super.call();
  82.         System.out.println("直立样式手机");
  83.     }
  84. }
  85. public class Client {
  86.     public static void main(String[] args) {
  87.         Phone vivo_phone = new FoldedPhone(new Vivo());
  88.         vivo_phone.open();
  89.         vivo_phone.call();
  90.         vivo_phone.close();
  91.         System.out.println("====================");
  92.         Phone xiaoMi_phone = new UpRightPhone(new XiaoMi());
  93.         xiaoMi_phone.open();
  94.         xiaoMi_phone.call();
  95.         xiaoMi_phone.close();
  96.     }
  97. }
复制代码
桥接模式在JDBC源码分析

  1. package com.mysql.cj.jdbc; //这是mysql包下的Driver
  2. public class Driver extends NonRegisteringDriver implements java.sql.Driver {
  3.     //
  4.     // Register ourselves with the DriverManager
  5.     //
  6.     static {
  7.         try {
  8.             //注册mysql驱动
  9.             java.sql.DriverManager.registerDriver(new Driver());
  10.         } catch (SQLException E) {
  11.             throw new RuntimeException("Can't register driver!");
  12.         }
  13.     }
  14.     /**
  15.      * Construct a new driver and register it with DriverManager
  16.      *
  17.      * @throws SQLException
  18.      *             if a database error occurs.
  19.      */
  20.     public Driver() throws SQLException {
  21.         // Required for Class.forName().newInstance()
  22.     }
  23. }
复制代码
说明,MySQL有自己的ConnectionImpl类,MySQL的Driver和Connection之间是通过DriverManager(java.sql)的类进行桥连接的。
UML图


注意事项和细节

桥接模式应用场景举例

1.对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用.
2.常见的应用场景:
只是为了记录自己的学习历程,且本人水平有限,不对之处,请指正。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4