首页 / 设计模式 / Java设计模式-桥接模式
Java设计模式-桥接模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java设计模式-桥接模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2789字,纯文字阅读大概需要4分钟。
内容图文
桥接模式
介绍
实现
步骤 1
1
2
3
|
public
interface
DrawAPI {
public
void
drawCircle(
int
radius,
int
x,
int
y);
}
|
步骤 2
1
2
3
4
5
6
7
|
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 +
"]"
);
}
}
|
1
2
3
4
5
6
7
|
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: "
+x+
", "
+ y +
"]"
);
}
}
|
步骤 3
1
2
3
4
5
6
7
|
public
abstract
class
Shape {
protected
DrawAPI drawAPI;
protected
Shape(DrawAPI drawAPI){
this
.drawAPI = drawAPI;
}
public
abstract
void
draw();
}
|
步骤 4
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
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;
}
public
void
draw() {
drawAPI.drawCircle(radius,x,y);
}
}
|
步骤 5
1
2
3
4
5
6
7
8
9
|
public
class
BridgePatternDemo {
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();
}
}
|
步骤 6
1
2
|
Drawing Circle[ color: red, radius:
10
, x:
100
,
100
]
Drawing Circle[ color: green, radius:
10
, x:
100
,
100
]
|
原文:https://www.cnblogs.com/zhuxiaopijingjing/p/12340321.html
内容总结
以上是互联网集市为您收集整理的Java设计模式-桥接模式全部内容,希望文章能够帮你解决Java设计模式-桥接模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。