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

一、UML类图

在这里插入图片描述

二、代码示例

  • Client:
public class Client {
    public static void main(String[] args) {
        Implementor implementor = new ConcreteImplementor();
        Abstraction abstraction = new RefinedAbstraction();
        abstraction.setImplementor(implementor);
        abstraction.operation();
    }
}
  • Abstraction:
public abstract class Abstraction {
    protected Implementor implementor;

    protected Implementor getImplementor() {
        return implementor;
    }

    protected void setImplementor(Implementor implementor) {
        this.implementor = implementor;
    }

    public abstract void operation();
}
  • RefinedAbstraction:
public class RefinedAbstraction extends Abstraction{
    @Override
    public void operation() {
        implementor.operationImp();
    }
}
  • Implementor:
public interface Implementor {
    void operationImp();
}
  • ConcreteImplementor:
public class ConcreteImplementor implements Implementor{
    @Override
    public void operationImp() { }
}

三、角色介绍

  • Client(客户端):
    客户端角色,程序的调用者。
  • Abstraction(抽象化):
    保存Implementor角色,并使用Implementor角色的方法定义基本的功能。
  • RefinedAbstraction(改善后的抽象化):
    在Abstraction角色的基础上增加新功能。
  • Implementor(实现者):
    定义用于实现Abstraction角色的接口的方法。
  • ConcreteImplementor(具体实现者):
    负责实现在Implementor角色中定义的接口。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值