快速理解桥接模式
来源:互联网 发布:尔雅网络通识课 编辑:程序博客网 时间:2024/06/02 04:14
桥接模式的目的是:分离抽象化和实现,使两者的接口可以不同,目的是分离。
桥接模式的角色有:
实现(Implementor):定义具体行为,具体特征的应用接口。
具体实现(ConcreteImplementor):实现Implementor。
目标接口(Target): 包含实现具体行为、具体特征的Implementor接口或者类。
桥接模式通过在Target里的操作,调用Implementor得操作,达到了Target的抽象与Implementor实现的分离。
如例程:Painter(Target)有画图形DrawShape操作,Painter里面包含了Shape(Implementor)接口,Painter的实现类RefinedPainter在自己的DrawShape操作中调用Shape的Draw和Paint操作。
通过例子,在来看看桥接模式起到的作用。在开发的时候设定Target和Implementor采用桥接机制,这样使得Target和Implementor的后续的接口定义和参数定义上有比较大的灵活度。打个比方,生成洗衣机有两个部门,一个是水管链接器,一个是生产进水管的。水管链接器部门就启动桥接模式,它告诉进水管生产部门,不用担心连不上,你们生成什么粗细的水管,都可以连上水龙头。这就在一开始就给生产进水管的部门很多自由度,实现了分离。
桥接模式与适配器模式在代码实现上是相同的。差别在于适配器模式是已经有一个进水管和家用水龙头存在了,这时候生产一个接头来连接进水管和水龙头,这个接头就是适配器。桥接是定下生产接头策略,再让进水管有开发的自由度。只是先后的问题。
试着写段程序:
- //Shape类
- abstract class Shape{
- public abstract void Draw();
- public abstract void Paint();
- }
- //具体工厂(每个具体工厂负责一个具体产品)
- class Circle extends Shape{
- public void Draw(){
- printfln("Draw Circle");
- }
- public void Paint(){
- printfln("Paint Circle");
- }
- }
- class Triangle extends Shape{
- public void Draw(){
- printfln("DrawTriangle ");
- }
- public void Paint(){
- printfln("PaintTriangle ");
- }
- }
- public abstract class Painter{
- private Shape SP;
- public SetImplementor(Shape sp){
- SP = sp;
- }
- public void DrawShape();
- }
- public class RefinedPainter extends Painter
- public void DrawShape(){
- SP.Draw();
- SP.Paint();
- }
- }
- //测试
- public class Test{
- public static void main(String[] args){
- RefinedPainter refinedPainter = new RefinedPainter ();
- refinedPainter.SetImplementor(new Circle());
- refinedPainter.DrwaShape();
- refinedPainter.SetImplementor(new Triangle());
- refinedPainter.DrwaShape();
- }
- }
0 0
- 快速理解桥接模式
- 观察者模式快速理解
- 快速理解观察者模式
- 快速理解MVC模式
- 快速理解代理模式
- 快速理解工厂类模式
- 快速理解装饰者模式
- 快速理解动态代理模式
- 通俗快速理解几个常用设计模式
- 快速理解简单工厂类模式
- 快速理解单件SINGLETON模式
- 快速理解Java中的五种单例模式
- thinkphp mvc模式快速理解应用
- 快速理解设计模式之创建型设计模式
- java 设计模式--适配器模式(实例快速理解)
- 桥接模式概念和理解
- 关于桥接模式的理解
- 快速理解Oracle归档模式的命令及参数
- 微信小程序-它是啥?了解了解
- zynq 常见缩写术语
- Linux开启CoreDump功能
- C#中的引用传递、值传递
- 开始写博文
- 快速理解桥接模式
- RTMP学习(三)RTMP协议详解(转)
- 基于ambari 2.4.1 HDP-2.5.0 构建安全的hive数据库
- 静态广播和动态广播
- bzoj3123 【SDOI2013】森林 启发式合并
- 解决Maven下载依赖慢的问题
- java socket参数详解:OOBInline和UrgentData
- C# 动态对象(dynamic)的用法
- 在Solr6.3中创建core