桥接模式
定义
将抽象和实现解耦,使他们可以独立变化。用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。
- Abstraction:抽象化角色,主要职责是定义出该角色的行为,同事保存一个对实现化角色的引用。
- Implementor:实现化角色,定义角色必需的行为和属性。
- RefinedAbstraction:修正抽象化角色,引用实现划角色对抽象化角色进行修正。
- ConcreteImplementor:具体实现化角色,实现接口或抽象类定义的方法和属性。
抽象角色引用实现角色,或者说抽象角色的部分实现是由实现角色完成的。
代码实现
1 | public interface Implementor { |
优点
- 抽象和实现分离,扩展能力好。
使用场景
- 不希望或不适用使用继承的场景。
- 接口或抽象类不稳定的场景。
- 重要性要求较高的场景。
代码:GitHub
欢迎关注公众号: