|
《Java实现简单责任链模式》场景:客户的购房折扣申请,销售、销售经理、销售总监、CEO依次可以批准不同粒度的折扣。
1 K& `: T$ ^, i2 ?, Z0 F" t* o% m0 T1 C
部分java源码: J' C4 @( H" J0 d! v% x
1、+ W* Y6 D6 d" ?+ Q. @1 ^4 B
package com.javazx.cor.handler;
+ d6 j6 V( V* _& m2 M% K* W- ~
! F2 B. L4 z c+ ]/**/ Z g8 ?1 @5 P: d& W6 @
* @author: itmrchen3 ~# D& ]* Y4 w- {: p' K. q
* @Description: 价格处理人 负责处理客户的折扣请求
d" b8 `& w. l( a. [9 S * @date 2019/8/1 1:14
. s- w' S( @. r. E/ Y5 w: h' ^ */
* o7 A( v/ `% ~: l$ L0 `2 lpublic abstract class PriceHandler {; U9 ?% Z3 |) p |8 [
protected PriceHandler successor;
% r# h6 H% S( I5 r
Q E5 |. a+ _( a8 B" J public void setSuccessor(PriceHandler successor) {
0 _8 P0 F& j, A2 L this.successor = successor;
& q8 R$ Q2 r5 T/ z }
& {/ q. `' i. X. I' \; e
/ x1 M7 |, Z1 i/ U) i7 z3 @ /**
& V. d( A3 W% G$ O * 处理折扣请求8 e' e5 \! P W+ ]. @6 i( s& [
*. {% q" B* J+ A7 e5 m6 I8 P) F# K
* @param discount9 a) E. J- o- N5 n
*/
: t( H9 ]# E. D) q3 b& ipublic abstract void processDiscount(float discount);
" z& p, G; ^" `7 v6 s) i# j* z! F* ?' v/ c: Z9 N$ ~. I+ P* _
/**
- N0 Z0 G/ ?& H3 q/ J- J * 创建PriceHandler的工厂方法
D% v# L. {2 R0 U6 l5 F *
b/ V( ?# d, D * @return! D) y5 u# Q: b7 R. i; c! c
*/# G, O# m3 K, r, e# G$ T P
public static PriceHandler createPriceHandler() {
8 ^6 N8 D) J" ~$ {6 d PriceHandler sales = new Sales();8 O9 `# m& l+ c Q
PriceHandler man = new Manager();5 c- m9 M. f1 e% a+ d: h" V- R4 `! c' g
PriceHandler dir = new Director();# R7 ~- j+ P0 i* @+ E8 Z
PriceHandler ceo = new Ceo();4 K! }; o. P/ t# `3 f
sales.setSuccessor(man);" H3 X; B6 B9 e. v6 s0 Q: L
man.setSuccessor(dir);! D0 K/ [( ?1 b( e" G$ ]6 j
dir.setSuccessor(ceo);
* r% @- I. D' O0 j8 J* ?+ ^2 |1 o return sales;3 ]8 b d2 z! g1 ~7 o5 N2 r5 Y
}
7 M( m; }6 P7 G! {/ o) D}
* Z1 A; z2 x% y( [) ?- r. I; P& n& ?* x, P4 S
2 V" J3 T" h% h1 P, y; v
2、
7 k- w% ^3 b$ Upackage com.javazx.cor.handler;7 b6 P! g7 w5 X
& b" h9 `8 \1 T% ^" I) X, B& D/**
2 ]5 }) L7 t }/ l T3 R * @author: itmrchen
6 V9 _$ I4 j9 `: d * @Description: 销售 可以批准5%以内的折扣/ ~. S! D0 u/ }: C$ B( Y
* @date 2019/8/1 1:19: i6 R f( Y6 o3 u
*/% B. |% S* k5 ?! \7 p$ V
public class Sales extends PriceHandler {2 w) F1 G4 ^) n2 {; g
@Override" ?0 e: L: a) E/ P" j
public void processDiscount(float discount) {
; T8 I1 r8 z; K if (discount <= 0.05) {" u2 G, i9 B0 s1 L" @; ~7 z1 O
System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
5 `0 n, g! j* S1 s- E1 `0 X ~ } else {2 r2 `, u! `6 e6 _7 R+ k" L
successor.processDiscount(discount);
- Z, V7 y9 p3 I }
1 q: X1 Y7 Q! k9 _ }/ u; S' J, B3 i# w$ f
}
7 z" H# m: r6 |& r4 J7 p; G
. h) X2 S! X$ h3、( P% l \9 n7 |% n
package com.javazx.cor.handler;- f: c& E, G2 e- \
# @" T9 P4 m: U+ s! `& N/**: j' _, g$ J' ~* i3 z+ a. `4 R
* @author: itmrchen$ a- P' ]. `4 ]) g% y8 |( [: U
* @Description:销售经理可以批准30%以内的折扣6 P# U7 J% L& M, ]$ O
* @date 2019/8/1 1:24+ t# `: v T3 N
*/
7 R4 M; w3 w% r' I$ j. O9 X+ apublic class Manager extends PriceHandler {
. V+ m* O1 Q3 v4 {& i @Override- k' n# L4 c* L5 I9 X3 S: n
public void processDiscount(float discount) {
5 A; p4 ~* c0 M* Z( q if (discount <= 0.3) {$ R! X: H7 P" `8 i$ s
System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);3 L+ `# j9 m4 G5 k h$ \
} else {( Y: j0 \. }% j8 @: y/ x
successor.processDiscount(discount);- ^$ f6 ~& F+ J) E
}& k m$ {, L! B& w
}% b/ V# F' X) z# j1 G9 l6 P) M
}' E- k3 V6 r: M
: y9 j+ z' o4 V7 h- m4 b4、$ l) c9 e! i0 }( H3 @7 Q( h
package com.javazx.cor.handler;8 s( N9 G6 v6 g
- R6 w8 F; R3 s" ^* L3 Q8 i/**
& y( T0 u+ K2 X* A * @author: itmrchen
Q1 I" S5 o) e0 a. ]1 g * @Description: 销售总监可以批准40%以内的折扣
. _5 Z; C* T! F# n0 K9 q2 h * @date 2019/8/1 1:26
- w3 |0 [! s/ W( |6 I% z8 n */, b* q9 m+ }1 E! C4 _
public class Director extends PriceHandler {0 d# M6 q3 d0 j: N5 y
@Override
7 ^, ~, U+ j* I8 P/ |& I public void processDiscount(float discount) {
/ A7 Q" a4 ]! ]6 p' t2 w; c if (discount <= 0.4) {
" L3 g- f/ O3 Y' _5 A System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
1 b# S% F8 W1 V# l7 V9 w } else {2 h. p) E, z" l f4 D
successor.processDiscount(discount);
; _3 K5 j1 |" \- e/ [- c }2 q- Q4 ?9 S3 c5 H, ~& d
}
" z) b/ y! D+ u( Y}
) w1 w6 A+ X" @: d1 ? f; R
- X+ w) B( x# r+ \* N: Q# J5、
9 Y& m/ Q# L: Z( a# [2 Mpackage com.javazx.cor.handler;
: [ O# H# a6 @: ~$ a' x3 P3 J1 v( |; a4 @- x
/**) c& Y; @/ ]4 j7 N% g
* @author: itmrchen( P8 Z5 v$ {. [( {0 H! r
* @Description:CEO有权拒绝超过55%的折扣
9 D5 R! I8 B8 o4 K( Z* @date 2019/8/1 1:28
, X/ e# w0 f3 w5 Y5 N* |3 Q( e5 c */
5 n: y# q# y/ J; G& `7 @public class Ceo extends PriceHandler {# g }0 l8 ~# H8 e1 w( ?0 s" y3 Y- Z' u
@Override
6 e7 W5 \0 x# F0 Q, ]0 [ public void processDiscount(float discount) {
8 R0 {: B% W9 k' w8 m: O- g( o if (discount <= 0.55) {
k' L# L8 t5 Q3 [ System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);# Y, Q* g ?3 s" [' ^, e% N
} else {
; J; w( P, O* C System.out.format("%s拒绝了折扣:%.2f%n", this.getClass().getName(), discount);
7 Z! e p; g3 ~7 ~ [' a }
! D# T" q$ z7 K. W/ e- m! d }' p# u$ o# u9 `% `
}
: K( l( ~! ?! j
9 M0 I- o' Y3 Z2 N) }) }2 l$ S; [8 n3 R- n- U
6、6 V# ?; N7 k9 C3 w$ N( w
package com.javazx.cor;
: j: y" m2 k& i4 l2 P+ |1 }: }9 L! x
1 T* ^" @0 v; s, d7 }( Wimport com.javazx.cor.handler.PriceHandler;: y2 ?1 ]5 [: ^
! T! z8 k5 p0 g, T+ C/**) }9 r0 K7 _: D7 u/ U0 u
* @author: itmrchen8 F$ M; M8 i: }
* @Description: 客户请求折扣+ Y0 W3 C6 [) @. Y7 _# `
* @date 2019/8/1 1:32
& V# o- @" k" L ` */
& e) m) q3 H" H8 T& Wpublic class Customer {0 E# q- B5 \1 a( g, Y1 K
private PriceHandler priceHandler;
" i2 b4 ?" d8 S2 c6 @
- w1 q2 m! R1 ~) L- o2 ]& L public void setPriceHandler(PriceHandler priceHandler) {! u) Z4 _! j: }7 F9 [
this.priceHandler = priceHandler;* D8 k5 d, P* ^5 `3 [4 C7 m
}
; M( S' v0 Y) X8 I0 e, e u" Q, P) F$ @4 r2 E6 T) o
public void requestDiscount(float discount) {! R" u( a- n: z6 I
priceHandler.processDiscount(discount);$ ?$ b* Y0 _ g2 u3 ~$ y. Y2 I
}5 ^0 e5 v: O! V; Z: \
! p' M6 }0 i: r8 C public static void main(String[] args) {
0 A" ?, X0 K+ F Customer customer = new Customer();
; r1 R& x7 {0 v6 G customer.setPriceHandler(PriceHandler.createPriceHandler());
( M. W3 o* m7 G3 W9 B customer.requestDiscount(0.01f);
9 ?6 W9 a/ l- K3 l: [# x6 a }: e4 @: P t& F5 [4 E* X
}
2 W! ~) P3 P. s4 ~/ I1 S6 z4 o
& |9 v6 K3 d C& H8 ?" k2 ?* ^7 }6 k, e; Z) a4 E
% h' _$ e8 h# Z0 p
5 N9 v, y$ w2 F* y+ Y- e$ U
5 Y2 J# ~! l+ X5 {: N5 r+ @& _! q6 n
github地址:https://github.com/itmrchen/springPro 位置在 springPro\src\main\java\com\javazx\cor包下面. Q3 k* ~' m9 P) H4 c
- b1 n5 b: u" B) p
& b) b. T& v% X! l0 E/ \9 i8 F' a( E8 J5 [3 V3 h
' o5 b9 W, h! J. a
* G4 O0 d, f( j; o. h7 `
|
|