文章目录
设计模式是面向问题、场景而总结产生的设计思路。是解决问题的套路。23 种经典的设计模式。它们又可以分为三大类:创建型、结构型、行为型。
结构型 包含了 代理模式、 桥接模式、 装饰器模式、适配器模式、门面模式、组合模式、 享元模式。
桥接模式
桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。
1. 为什么使用桥接模式
桥接模式使用组合代替继承,使系统更加灵活,并易于扩展,同时有效控制了系统中类的个数。
在很多情况下,桥接模式可以取代多层继承方案,多层继承方案违背了“单一职责原则”,复用性较差,且类的个数非常多,桥接模式是比多层继承方案更好的解决方法,它极大减少了子类的个数。
如果某个类存在两个独立变化的维度,可以运用桥接模式将这两个维度分离出来,使两者可以独立扩展,让系统更加符合“单一职责原则”。桥接模式提高了系统的可扩展性,在两个变化维度中任意扩展一个维度,都不需要修改原有系统,符合“开闭原则”。
2.使用桥接模式
桥接模式定义了四个角色,
Abstraction,持有Implementor,Implementor是代表了另一个维度。
RefinedAbstraction,继承Abstraction,因此可以调用在Implementor中定义的业务方法。
Implementor,定义了业务功能。
ConcreteImplementor,具体实现Implementor接口。
2.1 Abstraction
Abstraction角色,比如电脑类,持有一个对象,是操作系统。
public abstract class Computer {
protected OperationSystem os;
public Computer(OperationSystem os) {
this.os = os;
}
public abstract void work();
}
2.2 RefinedAbstraction
比如苹果电脑、戴尔电脑等。
public class AppleComputer extends Computer{
public AppleComputer(OperationSystem os) {
super(os);
}
@Override
public void work() {
System.out.println("Apple 笔记本,开机启动");
os.run();
}
}
public class DellComputer extends Computer{
public DellComputer(OperationSystem os) {
super(os);
}
@Override
public void work() {
System.out.println("Dell 笔记本,开机启动");
os.run();
}
}
2.3 Implementor
定义操作系统类
public abstract class OperationSystem {
public abstract void run();
}
2.4 ConcreteImplementor
可以有linux、windows、macos等操作系统。
public class LinuxOperationSystem extends OperationSystem{
@Override
public void run() {
System.out.println("Linux操作系统,操作系统加载");
}
}
public class WindowsOperationSystem extends OperationSystem{
@Override
public void run() {
System.out.println("windows操作系统,操作系统加载");
}
}
public class MacOperationSystem extends OperationSystem{
@Override
public void run() {
System.out.println("Mac操作系统,操作系统加载");
}
}
2.5 将大家组合起来
由于两个维度的解离,可以实现各种组合。
public class Main {
public static void main(String[] args) {
Computer computer = new DellComputer(new WindowsOperationSystem());
computer.work();
Computer computer2 = new DellComputer(new LinuxOperationSystem());
computer2.work();
Computer computer3 = new AppleComputer(new MacOperationSystem());
computer3.work();
}
}
运行如下
Dell 笔记本,开机启动
windows操作系统,操作系统加载
Dell 笔记本,开机启动
Linux操作系统,操作系统加载
Apple 笔记本,开机启动
Mac操作系统,操作系统加载
总结
在使用桥接模式时,首先应该识别出一个类所具有的两个独立变化的维度,将它们设计为两个独立的继承等级结构,为两个维度都提供抽象层,并建立抽象耦合。
模式系列在github上有一个开源项目,主要是本系列博客的demo代码。https://github.com/forestnlp/designpattern
如果您对软件开发、机器学习、深度学习有兴趣请关注本博客,将持续推出Java、软件架构、深度学习相关专栏。
您的支持是对我最大的鼓励。