• 设计模式(七)桥接模式


    1、定义

      桥接模式是将抽象部分与它的实现部分分离,使它们都对立地变化。它是一种对象结构模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

    2、优劣分析

    (1)好处分析

    • 桥接模式类似于多继承方案,但是多继承方案违背了类的单一职责原则,复用性比较差,类的个数也非常多,桥接模式是比多继承方案更好的解决方法。极大地减少了子类的个数,从而降低管理和维护的成本;
    • 桥接模式提高了系统的可扩展性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统,符合开闭原则,就像一座桥,可以把两个变化的维度链接起来。

    (2)劣势分析

    • 桥接模式的引入会增加系统的理解与设计维度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程;
    • 桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围具有一定的局限性。

    3、最佳实践

    • 如果一个系统需要在构建的抽象化角色和具体角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使他们在抽象层建立一个关联关系。抽象化角色和实现化结束可以以继承的方式独立扩展而互不影响,在程序运行时,可以动态将一个抽象化子类的对象和一个实现化子类的对象进行组合,即系统需要对抽象化角色和实现化角色进行动态耦合;
    • 一个类存在两个独立变化的维度,且这两个维度都需要进行扩展;
    • 虽然在系统中使用继承是没有问题的,但是由于抽象化角色和具体化角色需要独立变化,设计要求需要独立管理这两者。对于那些不需要使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。

    4、场景:

    • Java语言通过Java虚拟机实现了平台的无关性;
    • AWT中的Peer架构;
    • JDBC驱动程序也是桥接模式的应用之一。

    5、类图

     6、代码实例

    1 /**
    2  * @author it-小林
    3  * @desc 品牌
    4  * @date 2021年07月21日 19:41
    5  */
    6 public interface Brand {
    7 
    8     void info();
    9 }
     1 /**
     2  * @author it-小林
     3  * @desc 苹果品牌
     4  * @date 2021年07月21日 19:43
     5  */
     6 public class Apple implements Brand{
     7     @Override
     8     public void info() {
     9         System.out.print("苹果");
    10     }
    11 }
     1 /**
     2  * @author it-小林
     3  * @desc 联想品牌
     4  * @date 2021年07月21日 19:42
     5  */
     6 public class Lenovo implements Brand{
     7 
     8     @Override
     9     public void info() {
    10         System.out.print("联想");
    11     }
    12 }
     1 /**
     2  * @author it-小林
     3  * @desc 抽象的电脑类型类
     4  * @date 2021年07月21日 19:44
     5  */
     6 public abstract class Computer {
     7 
     8     //组合, 品牌
     9     protected Brand brand;
    10 
    11     public Computer(Brand brand) {
    12         this.brand = brand;
    13     }
    14 
    15     public void info(){
    16         //自带品牌
    17         brand.info();
    18     }
    19 }
     1 /**
     2  * @author it-小林
     3  * @desc
     4  * @date 2021年07月21日 19:46
     5  */
     6 public class Desktop extends Computer{
     7     public Desktop(Brand brand) {
     8         super(brand);
     9     }
    10 
    11     @Override
    12     public void info() {
    13         super.info();
    14         System.out.println("台式机");
    15     }
    16 }
     1 /**
     2  * @author it-小林
     3  * @desc
     4  * @date 2021年07月21日 19:48
     5  */
     6 public class Laptop extends Computer{
     7 
     8     public Laptop(Brand brand) {
     9         super(brand);
    10     }
    11 
    12     @Override
    13     public void info() {
    14         super.info();
    15         System.out.println("笔记本");
    16     }
    17 }
     1 /**
     2  * @author it-小林
     3  * @desc
     4  * @date 2021年07月21日 19:49
     5  */
     6 public class Test {
     7 
     8     public static void main(String[] args) {
     9         //苹果笔记本
    10         Computer computer = new Laptop(new Apple());
    11         computer.info();
    12 
    13         //联想台式机
    14         Computer computer2 = new Desktop(new Lenovo());
    15         computer2.info();
    16     }
    17 }

    运行结果截图

    如本文有侵权行为,请及时与本人联系,多多包涵! 小生初出茅庐,多多指教!

    本文来自博客园,作者:it-小林,转载请注明原文链接:https://www.cnblogs.com/linruitao/p/15041100.html

  • 相关阅读:
    优酷视频下载,优酷真实地址解析
    vim配置入门,到豪华版vim配置
    python字典实现三级菜单
    python 购物车
    来到博客园的第一天
    python urllib模块使用详解
    ubutun升级命令
    SQL注入笔记
    Ajax_showHint() 函数
    轮播图中遇到的问题
  • 原文地址:https://www.cnblogs.com/linruitao/p/15041100.html
Copyright © 2020-2023  润新知