Java实现(07)——桥接模式(Bridge Pattern)
来源:互联网 发布:人工智能的文献综述 编辑:程序博客网 时间:2024/05/16 11:17
桥接模式用于连接两个不相干的抽象实现,这种模式中存在一个当做桥梁的接口,本例展示可以用不同的颜色画一个圆,但是提供相同的抽象接口。
public interface DrawAPI { public void drawCircle(int radius, int x, int y);}
public class RedCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]"); }}
public class GreenCircle implements DrawAPI { @Override public void drawCircle(int radius, int x, int y) { System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + ", " + y + "]"); }}
public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI) { this.drawAPI = drawAPI; } public abstract void draw();}
public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } @Override public void draw() { drawAPI.drawCircle(radius, x, y); }}
public class Demo { public static void main(String[] args) { Shape redCircle = new Circle(100,100, 10, new RedCircle()); Shape greenCircle = new Circle(100, 100, 10, new GreenCircle()); redCircle.draw(); greenCircle.draw(); }}输出:
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: , 100]
0 0
- Java实现(07)——桥接模式(Bridge Pattern)
- java设计模式——桥接模式(Bridge Pattern)
- Java Bridge Pattern(桥接模式)
- (C++实现)——桥模式(Bridge Pattern)
- Java设计模式——桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式 Bridge Pattern
- 桥接模式(Bridge Pattern)
- 桥接模式 Bridge Pattern
- 桥接模式( Bridge Pattern)
- 桥接模式(Bridge Pattern)
- 桥接模式(Bridge Pattern)
- bridge pattern -- 桥接模式
- Java被忽略的基本知识(一)
- Java/Android 常面面试题抽取
- 深入理解js的prototype以及prototype的一些应用
- H5常用的标签元素
- 使用oci封装库ocilib访问oracle
- Java实现(07)——桥接模式(Bridge Pattern)
- 怎样发布一个工程到自己的GitHub
- bzoj 2733 [HNOI2012]永无乡 splay启发式合并
- 我的JavaScript成长之路——知识点梳理
- excel 与 latex合作
- 安卓标题栏随屏幕滑动改变颜色
- Android L 沉浸式状态栏和圆形动画实现炫酷效果
- ORACLE会话连接进程三者总结
- 两个数据库的问题