|
《Java实现简单责任链模式》场景:客户的购房折扣申请,销售、销售经理、销售总监、CEO依次可以批准不同粒度的折扣。
( q/ g+ a9 o% g* e9 e p& }9 r1 ^8 h* Z
部分java源码:. o/ X+ P. ~+ ]! u0 T
1、: B d9 g7 _, F! s8 l% t4 y, E& r
package com.javazx.cor.handler;/ s, \8 x8 Q/ i' \
& P& A N" M. l4 S1 c. M& U
/**6 r, ~) Y8 _6 o% I1 G
* @author: itmrchen/ _$ C" s8 e3 k* p
* @Description: 价格处理人 负责处理客户的折扣请求
5 J; H8 ]$ K, B4 ]. g1 M( ^ * @date 2019/8/1 1:14
) p1 H; \6 J! V0 u */
+ T+ e( M) C# l: J# V2 fpublic abstract class PriceHandler {
7 ~& u) v0 p3 c4 L/ c1 L3 G protected PriceHandler successor;6 T- W( [1 X4 _# c, ^2 t8 d/ Y
! [! \) ?2 w! S( A+ |" t6 H
public void setSuccessor(PriceHandler successor) {
8 Q; {' A& Y6 x+ j9 g( M1 O this.successor = successor;
5 u, Y* e& r; \5 u4 F }) H6 I: i$ c$ |2 A
- f! Z6 c+ G( l# e6 k8 F: g) R% g /**9 M* O; c6 E! U. I! r1 S; D! A |' O
* 处理折扣请求' c$ z) {5 y H
*9 E! A, a9 I2 S" e' u5 M5 Y
* @param discount! P2 H# f$ ^' @, f/ u! ?5 e
*/ k- A% ^: I( M2 u0 ]" \# [: u
public abstract void processDiscount(float discount);
% l9 ?' ~8 G8 N: M+ R( b, _ @! p t1 R; w% v* r( q6 s
/**( e; g4 T* V; u, V7 L
* 创建PriceHandler的工厂方法
* o2 B9 e$ |$ F" Q *
9 r% L; E: m2 i * @return
2 f+ V5 |! ?# H8 O" o g' P2 A*/
, U, P& n9 d/ D- dpublic static PriceHandler createPriceHandler() {
* C$ N* S& k# D! Z. m0 q PriceHandler sales = new Sales();
! f1 O( y7 G3 J" j* x PriceHandler man = new Manager();. q0 q5 L; Y9 t& i) D& Y* O2 A
PriceHandler dir = new Director();
, a+ ]/ y8 I' P8 R& S1 {* e PriceHandler ceo = new Ceo();- `$ o, B! v% X" |
sales.setSuccessor(man);9 y; K- s" P& H: Y* x# j! @ M: }
man.setSuccessor(dir);, h. h k) V1 n0 a# v4 d, f
dir.setSuccessor(ceo);
7 o7 E. u3 R' `+ o: I$ N return sales;; y; o- K8 K6 {. j, j6 E( \/ F
}% l; t8 H+ m7 G" `' I
}
% M2 x. ], S5 e ~$ i% E7 `& j& l, X# k7 h8 ^6 v/ V) @
" R- N( H. j) e8 Z6 a9 ?
2、2 |6 G( F, ~' J& @; E l
package com.javazx.cor.handler;
- m2 c! r+ x$ ?4 N
6 a `. T0 @5 [6 _+ ?# s0 A! ]/**
5 M* [& v+ ]* x# ^/ \ }1 J * @author: itmrchen
1 y; [! ]5 s3 v# n/ W * @Description: 销售 可以批准5%以内的折扣
$ g! r2 N4 Z0 s' a( O, N* u( D * @date 2019/8/1 1:19
1 U ~6 I( G4 x4 K3 ?# ]0 M */
. Y+ s1 o9 w i0 X H4 D" x5 u' Wpublic class Sales extends PriceHandler {
7 D* g {7 H) n @Override- e4 [9 ]) i* P- C, x
public void processDiscount(float discount) {
) n; t, R ~/ ] if (discount <= 0.05) {
% `- J$ A; ~. F& D, k System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);! E6 S; f# A9 Z
} else {
`5 D, i2 i4 U) Z successor.processDiscount(discount);
8 K6 H, @; T5 f/ z% U' i }
4 \' e) |) d! Z; b+ Z8 `5 C }: I" V* M8 a o4 @ R
}) j' h Q) y8 ^( r- [- Z* k
Q( r9 L b( A$ [1 k4 q% ]1 p+ _7 D5 K
3、
! ~3 \$ m8 o3 B6 \+ K" E0 jpackage com.javazx.cor.handler;
% {/ \/ S+ U5 Y; d) s9 z- S5 {! p) F
% h" m1 U. ]! p, d* p8 g4 j/**% B f, {* a' B3 R- S
* @author: itmrchen
6 _% M3 v0 b- f2 U6 f8 i. ^% R; S * @Description:销售经理可以批准30%以内的折扣" v' F+ [4 @, f: C1 j
* @date 2019/8/1 1:24* ?4 U! Q. }0 h3 |: `+ F3 c) d. [ c1 B
*/3 p" w; M: q% u$ F
public class Manager extends PriceHandler {0 p8 } e) _% G" q3 U) o, [& U
@Override) g+ u; ^8 O# ^& A
public void processDiscount(float discount) {4 |7 B ~: L; P3 k. b
if (discount <= 0.3) {
2 @& D2 G0 o$ t4 ]* c System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
9 Q( d. G3 ?' B2 h* h1 X" z } else {
0 z5 h6 z/ @. k) T% a successor.processDiscount(discount);& w, s; K @6 U% h6 Z
}
! U8 P( t3 Z) z8 `/ B, i8 j7 ^7 o }+ |( v0 s8 I+ X* \5 f: D4 d# l0 u
}
$ d U% X- C' m8 k' a) v w, q. Z- J+ H+ v( E& e b
4、8 F0 Z4 R9 h$ N2 c( V5 `, i
package com.javazx.cor.handler;1 J2 ?3 V/ H' t, e/ M3 v U: B
* v9 [, y2 ^" Q0 i7 r4 Z. _0 [
/**
3 w7 C( C" _* J9 x- S0 z+ P/ } * @author: itmrchen
9 \7 x; A+ U$ b * @Description: 销售总监可以批准40%以内的折扣) a& a" Z, n& n- j0 ?
* @date 2019/8/1 1:266 P8 o* d# X2 I+ [
*/
$ J+ ~& n) a8 W* b# v7 @/ \5 ppublic class Director extends PriceHandler {( l. b- z% B: m- M" d+ h! q+ _
@Override0 E% \2 v. r! P: U
public void processDiscount(float discount) {1 K+ L( C* Y* _ Y0 }
if (discount <= 0.4) {
( ]1 d# ^8 k# {; n/ Y5 k System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
) y/ S" A( B) C } else {" S) X3 c8 ?# W& J# O+ r: u% {4 n# y
successor.processDiscount(discount);
8 s! j0 a+ T% f$ G- X2 ] }
: [% n5 e/ r& p8 z }& g" t9 N9 W: H! S$ W
}. A! K0 `% I. B2 Y Q' u: v, L
: W" p- S* o; y* Y( a1 c8 S
5、
& L$ f! R% z( I9 r/ {; q9 jpackage com.javazx.cor.handler;
! v4 l) |) u1 }* }$ c7 T$ v) C7 r8 ~. I' X
/**
. n8 z. ^; d7 `4 l * @author: itmrchen9 X: J4 e# t3 O
* @Description:CEO有权拒绝超过55%的折扣- @6 p" `3 X0 G7 X
* @date 2019/8/1 1:284 c# ~ a- z4 _ c) C; X
*/' h3 d* Z+ P7 o; b% L6 c1 T
public class Ceo extends PriceHandler {
. ~* @. l6 J$ r7 r* b5 f( `6 Q( ~7 S# F @Override! w K# f) n! m5 M2 n% {" k! _
public void processDiscount(float discount) {
$ B0 [& ~, J! I) Q4 [, D* F if (discount <= 0.55) {
+ s1 f7 v. f5 D. a System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
$ w. a; `8 T) M" T, {8 L# L& C2 X } else {, `9 X- ]7 C* @6 t6 P) g
System.out.format("%s拒绝了折扣:%.2f%n", this.getClass().getName(), discount);
+ f* t- n, E$ P6 ^0 _, G y$ ^$ d }+ h) Y5 h" l% V) M: p+ _$ t0 o' k0 @1 u
}
0 q" J; l0 l8 \# q' N9 A: Q}
' Z! V m7 I" O
; ^+ l( }) k: z( N& {5 V/ g9 b7 U8 ^* `
6、
0 T( {8 e' t7 a. z: Cpackage com.javazx.cor;$ x/ Y2 L5 H) L
, ~& Z5 A3 f5 I8 bimport com.javazx.cor.handler.PriceHandler;, o. p/ R6 O( a3 v/ g1 w
; X0 M. b- @4 b: T. r& J) Q5 c
/**5 ~# v# P, o% j
* @author: itmrchen; K, t! j" P1 _
* @Description: 客户请求折扣* |1 R4 a8 E6 W
* @date 2019/8/1 1:32$ C: n2 ]- l" L# B8 V( ?
*/! T4 p. E: F# G! [3 L" S
public class Customer {! ]" j6 Z$ \8 ^: A& w! ?, y
private PriceHandler priceHandler;
. d1 U( X E+ X; J
% s; h5 N9 D' K8 k$ V public void setPriceHandler(PriceHandler priceHandler) {
! F1 n1 Z/ e4 Z) b# l this.priceHandler = priceHandler;
- d2 y0 f4 F7 p" X7 g) G }1 L" J( m3 n( R) _) q
% C5 J$ T# }* _( n; ?' Y! c+ k
public void requestDiscount(float discount) {" }, `3 w- b& t2 Q5 y" S' L
priceHandler.processDiscount(discount);- s$ o- O/ ~$ r; [) D9 ^* P0 G
}, B1 X% [2 \0 q' r0 Y0 n+ }# ~
) ~ o2 }2 V) i1 [- R6 S0 Q/ W
public static void main(String[] args) {+ ~2 |- v" _$ R
Customer customer = new Customer();
$ s6 } j p9 r7 @ \ customer.setPriceHandler(PriceHandler.createPriceHandler());% w% w( n' w* H8 f1 `
customer.requestDiscount(0.01f);; L# [6 z8 o! M9 w
}
. e) \, \* O5 K- ~% d( J3 S: t C2 w} Y" K7 Y) a) Q9 e5 e
# \1 R& |* C( q" X# e
; t/ c) j7 U9 a* T5 S: e8 g8 c/ l
# L3 |# D* z1 O+ {# S. y4 ?! [& I- {3 h+ c
; R- j$ B: B2 X" C' a
9 l$ U# E# W# {0 M7 S! tgithub地址:https://github.com/itmrchen/springPro 位置在 springPro\src\main\java\com\javazx\cor包下面
3 p2 B0 e$ g P" Z+ u& z! w
/ h; ~9 J; _) T# [2 W' ~
- S9 R4 l+ \' D+ c2 G1 _3 o% L6 n1 o6 o5 {" r5 ]6 j: k) ?
( H! ^; \0 V6 Z6 i2 |. x j* n- e7 f: w
|
|