This repository was archived by the owner on Apr 1, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +25
-8
lines changed
src/main/java/com/erudev/design/strategy/shopping Expand file tree Collapse file tree 3 files changed +25
-8
lines changed Original file line number Diff line number Diff line change 1+ package com .erudev .design .strategy .shopping ;
2+
3+ /**
4+ * Created by eru on 2020/1/23.
5+ */
6+ public class CashContext {
7+
8+ private CashSuper cs ;
9+
10+ public CashContext (CashSuper cashSuper ){
11+ this .cs = cashSuper ;
12+ }
13+
14+ public double getResult (double money ){
15+ return cs .acceptCash (money );
16+ }
17+ }
Original file line number Diff line number Diff line change 55 * Created by eru on 2020/1/23.
66 */
77public class CashFactory {
8- public static CashSuper createCash (String condition ){
9- CashSuper cs = null ;
8+ public static CashContext createCash (String condition ){
9+ CashContext cc = null ;
1010 switch (condition ){
1111 case "正常收费" :
12- cs = new CashNormal ();
12+ cc = new CashContext ( new CashNormal () );
1313 break ;
1414 case "满300返100" :
15- cs = new CashReturn ("300" , "100" );
15+ cc = new CashContext ( new CashReturn ("300" , "100" ) );
1616 break ;
1717 case "打8折" :
18- cs = new CashRebate ("0.8" );
18+ cc = new CashContext ( new CashRebate ("0.8" ) );
1919 break ;
2020 }
21- return cs ;
21+ return cc ;
2222 }
2323}
Original file line number Diff line number Diff line change 66 */
77public class Main {
88 public static void main (String [] args ) {
9- CashSuper cs = CashFactory .createCash ("打8折" );
10- double result = cs . acceptCash (300 );
9+ CashContext cc = CashFactory .createCash ("打8折" );
10+ double result = cc . getResult (300 );
1111 System .out .println ("result = " + result );
1212 }
1313}
You can’t perform that action at this time.
0 commit comments