美文网首页设计模式
桥接模式(Bridge)

桥接模式(Bridge)

作者: 剑道_7ffc | 来源:发表于2020-04-28 14:43 被阅读0次

一句话总结

多个维度

内容

桥接模式的核心是解耦抽象和具体,使两者独立变化;抽象和具体指两个独立变化的维度,其中抽象包含实现。抽象和实现共同完成同一个功能。

场景

消息按消息类型分为系统消息和邮件消息,按紧急程度分为普通消息和紧急消息。

类图

代码示例

// 抽象
public abstract class Abstraction {
    protected IImplementor mImplementor;
    public Abstraction(IImplementor implementor) {
        this.mImplementor = implementor;
    }
    public void operation() {
        this.mImplementor.operationImpl();
    }
}
// 修正抽象
public class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(IImplementor implementor) {
        super(implementor);
    }
    @Override
    public void operation() {
        super.operation();
        System.out.println("refined operation");
    }
}
// 抽象实现
public interface IImplementor {
    void operationImpl();
}
// 具体实现
public class ConcreteImplementorA implements IImplementor {
    public void operationImpl() {
        System.out.println("I'm ConcreteImplementor A");
    }
}
// 具体实现
public class ConcreteImplementorB implements IImplementor {
    public void operationImpl() {
        System.out.println("I'm ConcreteImplementor B");
    }
}
public class Test {
    public static void main(String[] args) {
        // 来一个实现化角色
        IImplementor imp = new ConcreteImplementorA();
        // 来一个抽象化角色,聚合实现
        Abstraction abs = new RefinedAbstraction(imp);
        // 执行操作
        abs.operation();
    }
}

源码分析

相关文章

  • 设计模式解析—桥接设计模式

    桥接模式(Bridge Pattern)定义和使用场景 定义桥接模式(Bridge Pattern):将抽象部分...

  • docker的三种网络模式

    桥接模式:bridge

  • 桥接模式

    设计模式:桥接模式(Bridge)

  • 结构型-桥接(Bridge)

    桥接(Bridge) [TOC] 定义 桥梁模式(Bridge Pattern)也叫做桥接模式,是一个比较简单的模...

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interface)模式或...

  • 10-桥接模式

    桥接模式-Bridge Pattern【学习难度:★★★☆☆,使用频率:★★★☆☆】 处理多维度变化——桥接模式(...

  • 桥接模式

    桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化。 桥接(Bridge)模式的优点是:由于...

  • 桥接模式-原理类图

    桥接模式(Bridge)-基本介绍 桥接模式(Bridge模式)是指:将实现与抽象放在两个不同的类层次中,使两个层...

  • 桥接模式(Bridge)

    定义它把事物对象和其具体行为、具体特征分离开来,使它们可以各自独立的变化。事物对象仅是一个抽象的概念。如“圆形”、...

  • 桥接模式-bridge

    将抽象部分与它的实现部分分离,使他们都可以独立地变化 效果及实现要点: 适用性: 在以下的情况下应当使用桥梁模式:...

网友评论

    本文标题:桥接模式(Bridge)

    本文链接:https://www.haomeiwen.com/subject/istvwhtx.html