描述
桥梁模式是一种相对简单的模式,通常以组合替代继承的方式实现。
从设计原则来讲,可以说是单一职责的一种体现。
将原本在一个类中的功能,按更细的粒度拆分到不同的类中,然后各自独立发展。
基本组件
该模式,通常包含:抽象化角色(持有实现化角色完成功能)、实现化角色、修正抽象化角色、具体实现化角色
- 实现化角色
public interface Implementor {
void implAction();
}
- 具体是实现化角色
public class MyImplementor implements Implementor {
@Override
public void implAction() {
System.out.println("MyImplementor action ....");
}
}
- 抽象化角色
public abstract class Abstraction {
private final Implementor implementor;
public Abstraction(Implementor implementor) {
this.implementor = implementor;
}
public void doAction() {
implementor.implAction();
action();
}
protected abstract void action();
}
- 修正抽象化角色
public class RefinedAbstraction extends Abstraction {
public RefinedAbstraction(Implementor implementor) {
super(implementor);
}
@Override
protected void action() {
System.out.println("RefinedAbstraction action ...");
}
}
使用
public class Sample {
public static void main(String[] args) {
Abstraction abstraction = new RefinedAbstraction(new MyImplementor());
abstraction.doAction();
}
}
桥梁模式&spm=1001.2101.3001.5002&articleId=147876260&d=1&t=3&u=bd14cda4173d4613bd317b2e84d83674)
776

被折叠的 条评论
为什么被折叠?



