Skip to content

Commit fca57f6

Browse files
committed
add chain of responsibility
1 parent 1dfc8f1 commit fca57f6

39 files changed

+114
-8
lines changed

Cargo.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ members = [
33
"composite",
44
"bridge_pattern",
55
"abstract_factory",
6-
"factory_method"
6+
"factory_method",
7+
"chain_of_responsibility"
78
]

chain_of_responsibility/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "chain_of_responsibility"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

chain_of_responsibility/src/lib.rs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
trait Request {}
2+
3+
struct ConcreteRequest;
4+
impl Request for ConcreteRequest {}
5+
trait RequestHandler: RequestHandlerClone {
6+
fn get_successor(&self) -> Option<Box<dyn RequestHandler>>;
7+
fn handle(&self, request: Box<dyn Request>) {
8+
match self.get_successor() {
9+
Some(successor) => successor.handle(request),
10+
None => println!("chain breaks here"),
11+
}
12+
}
13+
}
14+
15+
trait RequestHandlerClone {
16+
fn clone_box(&self) -> Box<dyn RequestHandler>;
17+
}
18+
impl<T> RequestHandlerClone for T
19+
where
20+
T: RequestHandler + Clone + 'static,
21+
{
22+
fn clone_box(&self) -> Box<dyn RequestHandler> {
23+
Box::new(self.clone())
24+
}
25+
}
26+
27+
impl Clone for Box<dyn RequestHandler> {
28+
fn clone(&self) -> Self {
29+
self.clone_box()
30+
}
31+
}
32+
33+
#[derive(Clone)]
34+
struct Widget {
35+
successor: Option<Box<dyn RequestHandler>>,
36+
}
37+
38+
impl RequestHandler for Widget {
39+
fn get_successor(&self) -> Option<Box<dyn RequestHandler>> {
40+
self.successor.clone()
41+
}
42+
43+
fn handle(&self, _request: Box<dyn Request>) {
44+
println!("handler in Widget");
45+
}
46+
}
47+
48+
#[derive(Clone)]
49+
struct Application {
50+
successor: Option<Box<dyn RequestHandler>>,
51+
}
52+
53+
impl RequestHandler for Application {
54+
fn get_successor(&self) -> Option<Box<dyn RequestHandler>> {
55+
self.successor.clone()
56+
}
57+
58+
fn handle(&self, _request: Box<dyn Request>) {
59+
println!("handler in application")
60+
}
61+
}
62+
63+
#[derive(Clone)]
64+
struct LazyOne {
65+
successor: Option<Box<dyn RequestHandler>>,
66+
}
67+
68+
impl RequestHandler for LazyOne {
69+
fn get_successor(&self) -> Option<Box<dyn RequestHandler>> {
70+
self.successor.clone()
71+
}
72+
}
73+
74+
#[cfg(test)]
75+
mod tests {
76+
use crate::{Application, ConcreteRequest, LazyOne, RequestHandler, Widget};
77+
78+
#[test]
79+
fn create_success_chain() {
80+
let application = Application { successor: None };
81+
let widget = Widget {
82+
successor: Some(Box::new(application)),
83+
};
84+
let request = Box::new(ConcreteRequest);
85+
widget.handle(request);
86+
}
87+
88+
#[test]
89+
fn create_break_chain() {
90+
let widget = Widget { successor: None };
91+
let lazy_one = LazyOne {
92+
successor: Some(Box::new(widget)),
93+
};
94+
let request = Box::new(ConcreteRequest);
95+
lazy_one.handle(request);
96+
}
97+
}

target/.rustc_info.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"rustc_fingerprint":7405054423634608984,"outputs":{"931469667778813386":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/siliang/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.56.1 (59eed8a2a 2021-11-01)\nbinary: rustc\ncommit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35\ncommit-date: 2021-11-01\nhost: x86_64-unknown-linux-gnu\nrelease: 1.56.1\nLLVM version: 13.0.0\n","stderr":""},"2797684049618456168":{"success":false,"status":"exit status: 1","code":1,"stdout":"","stderr":"error: `-Csplit-debuginfo` is unstable on this platform\n\n"},"15537503139010883884":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""}},"successes":{}}
1+
{"rustc_fingerprint":7405054423634608984,"outputs":{"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.56.1 (59eed8a2a 2021-11-01)\nbinary: rustc\ncommit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35\ncommit-date: 2021-11-01\nhost: x86_64-unknown-linux-gnu\nrelease: 1.56.1\nLLVM version: 13.0.0\n","stderr":""},"931469667778813386":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/siliang/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"2797684049618456168":{"success":false,"status":"exit status: 1","code":1,"stdout":"","stderr":"error: `-Csplit-debuginfo` is unstable on this platform\n\n"},"15537503139010883884":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""}},"successes":{}}

target/debug/.fingerprint/composite-86278353ed196e19/output-test-lib-composite

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7a79fa9567a51e5a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e04aad9e5b4e1988

target/debug/.fingerprint/composite-a83217463d2d0b07/output-lib-composite

-3
This file was deleted.

target/debug/incremental/composite-2ta8dnqsimakf/s-g7m0btkzka-14680bj.lock

Whitespace-only changes.

target/debug/incremental/composite-2ta8dnqsimakf/s-g7m0btnt0c-1qzl6ov.lock

Whitespace-only changes.

target/debug/incremental/composite-2ta8dnqsimakf/s-g7m0btr3sl-zwnip8.lock

Whitespace-only changes.

target/debug/incremental/composite-zv01n1qk40d1/s-g7m0btkzig-1cwn5hh.lock

Whitespace-only changes.

target/debug/incremental/composite-zv01n1qk40d1/s-g7m0btnt0b-w605tg.lock

Whitespace-only changes.

target/debug/incremental/composite-zv01n1qk40d1/s-g7m0btqrxn-ilg4oy.lock

Whitespace-only changes.

0 commit comments

Comments
 (0)