Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
ec9af416
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ec9af416
编写于
9月 23, 2015
作者:
M
Maxim Galushka
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#247 adding getAttackPower method to pattern decorator
上级
b3e4e8a4
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
15 addition
and
0 deletion
+15
-0
decorator/src/main/java/com/iluwatar/decorator/App.java
decorator/src/main/java/com/iluwatar/decorator/App.java
+2
-0
decorator/src/main/java/com/iluwatar/decorator/Hostile.java
decorator/src/main/java/com/iluwatar/decorator/Hostile.java
+2
-0
decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java
...ator/src/main/java/com/iluwatar/decorator/SmartTroll.java
+6
-0
decorator/src/main/java/com/iluwatar/decorator/Troll.java
decorator/src/main/java/com/iluwatar/decorator/Troll.java
+5
-0
未找到文件。
decorator/src/main/java/com/iluwatar/decorator/App.java
浏览文件 @
ec9af416
...
...
@@ -23,11 +23,13 @@ public class App {
Hostile
troll
=
new
Troll
();
troll
.
attack
();
troll
.
fleeBattle
();
System
.
out
.
printf
(
"Simple troll power %d.\n"
,
troll
.
getAttackPower
());
// change the behavior of the simple troll by adding a decorator
System
.
out
.
println
(
"\nA smart looking troll surprises you."
);
Hostile
smart
=
new
SmartTroll
(
troll
);
smart
.
attack
();
smart
.
fleeBattle
();
System
.
out
.
printf
(
"Smart troll power %d.\n"
,
smart
.
getAttackPower
());
}
}
decorator/src/main/java/com/iluwatar/decorator/Hostile.java
浏览文件 @
ec9af416
...
...
@@ -9,6 +9,8 @@ public interface Hostile {
void
attack
();
int
getAttackPower
();
void
fleeBattle
();
}
decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java
浏览文件 @
ec9af416
...
...
@@ -21,6 +21,12 @@ public class SmartTroll implements Hostile {
decorated
.
attack
();
}
@Override
public
int
getAttackPower
()
{
// decorated troll power + 20 because it is smart
return
decorated
.
getAttackPower
()
+
20
;
}
@Override
public
void
fleeBattle
()
{
System
.
out
.
println
(
"The troll calls for help!"
);
...
...
decorator/src/main/java/com/iluwatar/decorator/Troll.java
浏览文件 @
ec9af416
...
...
@@ -11,6 +11,11 @@ public class Troll implements Hostile {
System
.
out
.
println
(
"The troll swings at you with a club!"
);
}
@Override
public
int
getAttackPower
()
{
return
10
;
}
public
void
fleeBattle
()
{
System
.
out
.
println
(
"The troll shrieks in horror and runs away!"
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录