|
《Java实现简单责任链模式》场景:客户的购房折扣申请,销售、销售经理、销售总监、CEO依次可以批准不同粒度的折扣。
/ I! d0 K% u! S% T) Z: w- y# d$ L P7 Q
部分java源码:4 ?- H8 m9 m% u5 [
1、9 X# Q z5 E( `" }. D
package com.javazx.cor.handler;
+ n+ X4 J. O0 g. X7 w' |
! E% k: Y% V; v/**" r9 I @- d0 {
* @author: itmrchen
- B' H8 M( D1 C8 t$ n * @Description: 价格处理人 负责处理客户的折扣请求
3 j j5 P# P: V/ F. b+ Q h4 | * @date 2019/8/1 1:14
2 U% [% j& ?' B; [5 o" E4 B! r2 _4 ` */- K6 ?4 z! c8 y9 U$ [+ @
public abstract class PriceHandler {: X) Q# G/ `- u6 r9 V* n7 Q1 ^0 j
protected PriceHandler successor;
6 E# ^: x$ }0 A) f' s0 a8 w8 }
4 D3 L S) A/ } D+ ]) S$ V+ X public void setSuccessor(PriceHandler successor) {
0 }: }5 G' P3 t+ `8 t' ~ this.successor = successor;
% V$ u L; n6 E }5 l5 y2 f/ a- n% J- V8 G
3 {0 B5 P& }: i# E /**% N# U3 n1 P O) a9 m' C. B
* 处理折扣请求" O; s+ w; ?. W! ?# d
*# h7 ^/ U$ S' e
* @param discount' C8 O" r. d, F* |8 ?+ y
*/, x. [6 [( G b4 ?. R
public abstract void processDiscount(float discount);. T( o; K# z- E% ~
4 M! @" Q, T. e; [% [' A
/**
; m- V( P2 o3 M- | * 创建PriceHandler的工厂方法! D, a" @9 W9 `$ f) A# L
*
$ C1 j$ n" e+ L * @return
$ i- k0 w# q9 i/ P*/
' L+ a5 v; U- h6 ^# Cpublic static PriceHandler createPriceHandler() {& h7 T6 F6 X" O
PriceHandler sales = new Sales();
: Y" _- M6 h( ~6 l! N! t/ ~ PriceHandler man = new Manager();, S7 W7 D5 E& n, D
PriceHandler dir = new Director();
+ u# k. y" L% b' r2 s PriceHandler ceo = new Ceo();/ j. U. |0 _5 h
sales.setSuccessor(man);
8 A2 M" T2 ] n$ J% w7 O man.setSuccessor(dir);; \7 [$ n8 j$ k
dir.setSuccessor(ceo);
# W1 @4 R7 y2 q) ~: N9 k7 C return sales;
+ g3 Y2 ^, j; S; k+ [ }
! U" q1 y) i! ]" z4 w- {}
' U3 |0 I0 a6 t& H) z, H0 ]- V1 Q% E$ _
3 p6 K: A. g( E4 u* x1 V d- [: X+ b$ c6 |+ W/ k+ v, ?- d
2、
6 f* Q/ }' X2 P9 L1 q5 Ppackage com.javazx.cor.handler;
g8 \; d" ^3 H9 x6 d- ~4 d. m1 W) R" o, ]
/**
7 e" j- { G6 M5 j& r) G* j * @author: itmrchen# {7 A3 @" e+ J3 P+ C+ e
* @Description: 销售 可以批准5%以内的折扣+ m$ d; }( |! R, Y# W3 X
* @date 2019/8/1 1:191 H/ _6 f3 e: Q: m+ j. j
*/
7 s5 v9 u0 b) H2 _public class Sales extends PriceHandler {+ \6 s) N! U, K# \5 X
@Override
5 `' W! R& x o. {% I public void processDiscount(float discount) {
0 c* M8 u: c4 C if (discount <= 0.05) {0 ]5 q% t4 w g( Y
System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
~; ~/ M3 j- f& k } else {
6 m. a: c g6 U, x successor.processDiscount(discount);
8 y! _, K3 R. f2 G5 a' z }
; l! z# N: e) p' v3 G' l }
# x7 {7 P/ `* Y0 W- n; u}. s+ O9 r2 L% N/ q$ M8 G
* e( K# P& u# Q* Q9 h+ X8 I3、' u- o! N2 p B+ d
package com.javazx.cor.handler;
) ?6 Q1 U9 k; E+ c- @$ W* t9 l+ \ E- @: O1 X& D
/**- u0 m6 |8 x) o0 [# R% n
* @author: itmrchen
) z7 z/ K; B! k/ ?# H* F * @Description:销售经理可以批准30%以内的折扣5 K2 l# t5 ~" c
* @date 2019/8/1 1:24
1 j$ h+ w; X& a */
0 q# y! g1 q7 F4 ]) P' Cpublic class Manager extends PriceHandler {: | `& n2 r9 U7 ^5 v
@Override
: Y, u1 l+ K* o9 | public void processDiscount(float discount) {' ^5 W' g4 C q) k a
if (discount <= 0.3) {
4 X8 V" M: J, b) i' P# J4 Z System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
' `/ Y# A" W6 {# R0 I } else {
) v7 J: o9 A1 ^ successor.processDiscount(discount);" S7 o+ w- U0 L" `7 ]2 a& `
}2 |6 \7 w* Y6 a" @$ x
}
+ ~# o% Y3 I, W; J4 A}
. X4 O) h3 r7 O3 ?6 H2 Z0 \
$ s% i( q5 k9 A& l7 O/ {4、
: z+ i( W: J. v. Cpackage com.javazx.cor.handler;) A6 m1 X; ~- K$ V4 q5 A
$ _0 `$ x9 r- u8 \2 [- v. p
/**
. C" s3 B+ y4 D, M' w! ~9 A * @author: itmrchen
/ r) z1 Y3 `+ ^! {* q" j* U7 a * @Description: 销售总监可以批准40%以内的折扣
3 r1 W2 X4 U1 @ A& ?# x4 t * @date 2019/8/1 1:26
# w# W% a# s0 i7 N1 O, T */
& v) D% u0 l6 C% m# U& n6 M5 H- npublic class Director extends PriceHandler {& T, G& j8 Y1 V: P* i C/ q u
@Override
5 |- S5 [1 {; e1 d public void processDiscount(float discount) {
: A3 T4 u) d: j2 t5 q if (discount <= 0.4) {& _# _+ V( t$ L( d- m+ ?. W2 p
System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
, F% w; A$ f" a' t' L* H0 I } else {6 e0 k* t4 _6 O* Z* G
successor.processDiscount(discount);6 t; g. p6 e9 b) E7 d, c+ z
}
( v: e/ F* e( U* B$ V' F I6 K* Z }' X- _ r+ D+ V
}
" }9 ^. r9 k: [$ s
3 A0 c, f! w3 _ b, f: Y! b5、
1 L% j4 s2 E: v. I& f( Z. ~. spackage com.javazx.cor.handler;
; l% {( j9 {/ d) S% o$ r; Z
2 v3 a; s8 N' S2 y, }( }# x, J/**$ ~+ u7 ~( X! U9 @
* @author: itmrchen
) s) M) w3 Y: a! D" x5 W * @Description:CEO有权拒绝超过55%的折扣
: t% `9 b: v) f5 L; `0 r: C* @date 2019/8/1 1:28- P: t0 {4 P7 M+ |) t9 }
*/) l, Q) l; K# f1 l
public class Ceo extends PriceHandler {6 R) ?8 ?$ b+ p* o2 e6 G
@Override7 c9 v) H5 ?4 I9 a0 d ]6 w
public void processDiscount(float discount) {' \5 u; v! n& e0 V( V0 s! Y
if (discount <= 0.55) {5 c9 ]% }4 [. S2 B& w) B+ T- [: q
System.out.format("%s批准了折扣:%.2f%n", this.getClass().getName(), discount);
( {4 ~+ B$ C6 Q } else {. `2 N! ?! c- r6 M* i
System.out.format("%s拒绝了折扣:%.2f%n", this.getClass().getName(), discount);+ T0 Y* u8 _' n5 B7 W
}' F) v/ M/ p- H" t1 u
}
4 N4 a" x+ f" Q5 h}& Z+ d( p2 z, ]0 U- \8 b2 ~6 e
! f$ V& p$ [1 _4 q
$ ]9 Y1 ?, J4 @& F" P, g# R$ e6、& G5 w E0 T4 q' t
package com.javazx.cor;
r! E- `% X$ J9 T z) _0 a# v# I% x! A; q4 H3 c" s2 f) |! H1 @
import com.javazx.cor.handler.PriceHandler;
8 y' q7 D' d! L7 M# y
: w: J# {7 @ V/**
# y- R5 @( f9 {. z( i * @author: itmrchen
5 M5 u6 }) ~' A5 W# Z6 _4 L/ H * @Description: 客户请求折扣
: D$ q+ R: R9 a * @date 2019/8/1 1:32, z4 w# D3 J# I+ F2 x9 l- E
*/6 B7 h- f* d7 p: L* m! Q: X1 S: s
public class Customer {
/ r1 P7 B+ s$ S1 W private PriceHandler priceHandler;
# j2 B9 P$ j- c3 G' [' C
6 [. f( p/ x/ d" C7 n$ f4 U& L3 ~ public void setPriceHandler(PriceHandler priceHandler) {; h5 y$ b7 x/ J$ o, ~' W/ i# d: G
this.priceHandler = priceHandler;7 `' M2 T* ~, V- W) u ?7 b" h3 }
}
4 t/ l6 A) Q3 B/ c! J# o$ d/ n1 X- [ q4 o8 M' I
public void requestDiscount(float discount) {
, R3 h! a2 b+ X9 _/ e/ V priceHandler.processDiscount(discount); @: S! Z. K2 H( E* N
}
) G }9 Y; N9 @# b K/ ]. C' H3 V2 A2 h( g! ~. F; K& t& W5 N$ m
public static void main(String[] args) {
1 V$ r2 `1 s2 C9 k1 @ Customer customer = new Customer();
- B8 {# S4 }1 \% g customer.setPriceHandler(PriceHandler.createPriceHandler());! ^$ a5 D' t; g S
customer.requestDiscount(0.01f);
2 U. ]/ G' X5 c5 \3 v }
9 T! k& Q/ t$ q% l}
- z- }& \' D, Z# P
; E0 B! p: y3 f! ~4 H1 D. S
+ s5 ?9 \% |5 f E9 |+ A/ ?; ]- Y& B7 T' G* Q x n- J3 v2 _ o7 s# f
! s0 f2 m, M5 O$ A& b' O. K5 k1 T
( p0 _. u: N" O5 k
2 O; s4 x+ y! ^1 I6 Fgithub地址:https://github.com/itmrchen/springPro 位置在 springPro\src\main\java\com\javazx\cor包下面) b+ V% A3 ]9 C! s
0 F D9 X5 t7 u; G; N! d$ X* i
) j# C/ K5 i+ n, g
4 b. L" c* ]* A/ Z: ^
" T E! ]% k& z+ I+ \( ~; D: U2 k; i" c) [- M. f( X4 o6 Z
|
|