Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
9e7db125
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,发现更多精彩内容 >>
提交
9e7db125
编写于
8月 17, 2014
作者:
I
Ilkka Seppala
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added chain of responsibility sample
上级
a53c217f
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
189 addition
and
0 deletion
+189
-0
chain/pom.xml
chain/pom.xml
+23
-0
chain/src/main/java/com/iluwatar/App.java
chain/src/main/java/com/iluwatar/App.java
+14
-0
chain/src/main/java/com/iluwatar/OrcCommander.java
chain/src/main/java/com/iluwatar/OrcCommander.java
+22
-0
chain/src/main/java/com/iluwatar/OrcKing.java
chain/src/main/java/com/iluwatar/OrcKing.java
+19
-0
chain/src/main/java/com/iluwatar/OrcOfficer.java
chain/src/main/java/com/iluwatar/OrcOfficer.java
+23
-0
chain/src/main/java/com/iluwatar/OrcSoldier.java
chain/src/main/java/com/iluwatar/OrcSoldier.java
+22
-0
chain/src/main/java/com/iluwatar/Request.java
chain/src/main/java/com/iluwatar/Request.java
+33
-0
chain/src/main/java/com/iluwatar/RequestHandler.java
chain/src/main/java/com/iluwatar/RequestHandler.java
+23
-0
chain/src/main/java/com/iluwatar/RequestType.java
chain/src/main/java/com/iluwatar/RequestType.java
+9
-0
pom.xml
pom.xml
+1
-0
未找到文件。
chain/pom.xml
0 → 100644
浏览文件 @
9e7db125
<?xml version="1.0"?>
<project
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<groupId>
com.iluwatar
</groupId>
<artifactId>
java-design-patterns
</artifactId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<groupId>
com.iluwatar
</groupId>
<artifactId>
chain
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<name>
chain
</name>
<url>
http://maven.apache.org
</url>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
chain/src/main/java/com/iluwatar/App.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
App
{
public
static
void
main
(
String
[]
args
)
{
OrcKing
king
=
new
OrcKing
();
king
.
makeRequest
(
new
Request
(
RequestType
.
DEFEND_CASTLE
,
"defend castle"
));
king
.
makeRequest
(
new
Request
(
RequestType
.
TORTURE_PRISONER
,
"torture prisoner"
));
king
.
makeRequest
(
new
Request
(
RequestType
.
COLLECT_TAX
,
"collect tax"
));
}
}
chain/src/main/java/com/iluwatar/OrcCommander.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
OrcCommander
extends
RequestHandler
{
public
OrcCommander
(
RequestHandler
handler
)
{
super
(
handler
);
}
@Override
public
void
handleRequest
(
Request
req
)
{
if
(
req
.
getRequestType
().
equals
(
RequestType
.
DEFEND_CASTLE
))
{
printHandling
(
req
);
}
else
{
super
.
handleRequest
(
req
);
}
}
@Override
public
String
toString
()
{
return
"Orc commander"
;
}
}
chain/src/main/java/com/iluwatar/OrcKing.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
OrcKing
{
RequestHandler
chain
;
public
OrcKing
()
{
buildChain
();
}
private
void
buildChain
()
{
chain
=
new
OrcCommander
(
new
OrcOfficer
(
new
OrcSoldier
(
null
)));
}
public
void
makeRequest
(
Request
req
)
{
chain
.
handleRequest
(
req
);
}
}
chain/src/main/java/com/iluwatar/OrcOfficer.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
OrcOfficer
extends
RequestHandler
{
public
OrcOfficer
(
RequestHandler
handler
)
{
super
(
handler
);
}
@Override
public
void
handleRequest
(
Request
req
)
{
if
(
req
.
getRequestType
().
equals
(
RequestType
.
TORTURE_PRISONER
))
{
printHandling
(
req
);
}
else
{
super
.
handleRequest
(
req
);
}
}
@Override
public
String
toString
()
{
return
"Orc officer"
;
}
}
chain/src/main/java/com/iluwatar/OrcSoldier.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
OrcSoldier
extends
RequestHandler
{
public
OrcSoldier
(
RequestHandler
handler
)
{
super
(
handler
);
}
@Override
public
void
handleRequest
(
Request
req
)
{
if
(
req
.
getRequestType
().
equals
(
RequestType
.
COLLECT_TAX
))
{
printHandling
(
req
);
}
else
{
super
.
handleRequest
(
req
);
}
}
@Override
public
String
toString
()
{
return
"Orc soldier"
;
}
}
chain/src/main/java/com/iluwatar/Request.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
class
Request
{
private
String
requestDescription
;
private
RequestType
requestType
;
public
Request
(
RequestType
requestType
,
String
requestDescription
)
{
this
.
setRequestType
(
requestType
);
this
.
setRequestDescription
(
requestDescription
);
}
public
String
getRequestDescription
()
{
return
requestDescription
;
}
public
void
setRequestDescription
(
String
requestDescription
)
{
this
.
requestDescription
=
requestDescription
;
}
public
RequestType
getRequestType
()
{
return
requestType
;
}
public
void
setRequestType
(
RequestType
requestType
)
{
this
.
requestType
=
requestType
;
}
@Override
public
String
toString
()
{
return
getRequestDescription
();
}
}
chain/src/main/java/com/iluwatar/RequestHandler.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
abstract
class
RequestHandler
{
private
RequestHandler
next
;
public
RequestHandler
(
RequestHandler
next
)
{
this
.
next
=
next
;
}
public
void
handleRequest
(
Request
req
)
{
if
(
next
!=
null
)
{
next
.
handleRequest
(
req
);
}
}
protected
void
printHandling
(
Request
req
)
{
System
.
out
.
println
(
this
+
" handling request \""
+
req
+
"\""
);
}
@Override
public
abstract
String
toString
();
}
chain/src/main/java/com/iluwatar/RequestType.java
0 → 100644
浏览文件 @
9e7db125
package
com.iluwatar
;
public
enum
RequestType
{
DEFEND_CASTLE
,
TORTURE_PRISONER
,
COLLECT_TAX
}
pom.xml
浏览文件 @
9e7db125
...
...
@@ -30,6 +30,7 @@
<module>
facade
</module>
<module>
flyweight
</module>
<module>
proxy
</module>
<module>
chain
</module>
</modules>
<build>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录