责任链模式,是发送请求后,然后通过多个handler,知道有个handler来处理,然后再返回结果。
public
class
Handler {
private
Handler
handler
;
public
Handler getHandler() {
return
handler
;
}
public
void
setHandler(Handler handler) {
this
.
handler
= handler;
}
public
void
handlerRequest() {
if
(
this
.getHandler() !=
null
) {
this
.
handler
.handlerRequest();
}
}
}
public
class
Handler1
extends
Handler {
@Override
public
void
handlerRequest() {
super
.handlerRequest();
System.
out
.println(
" handler by handler1"
);
}
}
public
class
Handler2
extends
Handler {
@Override
public
void
handlerRequest() {
super
.handlerRequest();
System.
out
.println(
" handler by handler2"
);
}
}
public
class
Handler3
extends
Handler {
@Override
public
void
handlerRequest() {
super
.handlerRequest();
System.
out
.println(
" handler by handler3"
);
}
}
public
class
ChainClient {
public
static
void
main(String[] args) {
Handler h1 =
new
Handler1();
Handler h2 =
new
Handler2();
Handler h3 =
new
Handler3();
h2.setHandler(h1);
h3.setHandler(h2);
h3.handlerRequest();
}
}