首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >07 设计模式 桥接模式

07 设计模式 桥接模式

作者头像
shimeath
发布2020-07-30 17:06:16
发布2020-07-30 17:06:16
3790
举报

桥接模式

桥接模式,指的就是像桥一样将两个类关联起来,将抽象与实现分离,使得每个部分可以独立变化。

采用桥接设计模式之前如上图所示

采用桥接模式,将类分为品牌品牌下分为苹果联想等,电脑下分为台式机笔记本平板电脑

采用桥接模式之后就可以按照需要,自己进行组装

代码如下:

品牌接口

代码语言:javascript
复制
public interface Brand {
    public void info();
}
//联想牌
class Lenovo implements Brand {
    @Override
    public void info() {
        System.out.print("联想");
    }
}
//苹果牌
class Apple implements Brand {
    @Override
    public void info() {
        System.out.print("苹果");
    }
}

电脑接口

代码语言:javascript
复制
//电脑类
public class Computer {
    protected Brand brand;

    Computer(Brand brand){
        this.brand = brand;
    }

    public void info(){
        brand.info();
    }
}
//台式电脑
class Desktop extends Computer{

    Desktop(Brand brand) {
        super(brand);
    }

    @Override
    public void info() {
        super.info();
        System.out.print("台式机");
    }
}
//笔记本电脑
class Laptop extends Computer{

    Laptop(Brand brand) {
        super(brand);
    }

    @Override
    public void info() {
        super.info();
        System.out.print("笔记本");
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 桥接模式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档