Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit bf50d96

Browse files
committed
策略模式
1 parent 56716d7 commit bf50d96

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

src/main/java/com/erudev/design/strategy/shopping/CashFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
* Created by eru on 2020/1/23.
66
*/
77
public 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
}

src/main/java/com/erudev/design/strategy/shopping/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77
public 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
}

0 commit comments

Comments
 (0)