首页 > 代码库 > Chapter 2.策略模式
Chapter 2.策略模式
首先贴一段代码:
package xiao;
import java.util.Scanner;
class CashSuper{
private int num;
private double price;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double acceptCash(double money){
return money;
}
}
class CashNormal extends CashSuper{
}
class CashRebate extends CashSuper{
private double rebate;
public CashRebate(double rebate){
this.rebate = rebate;
}
public double getRebate() {
return rebate;
}
public void setRebate(double rebate) {
this.rebate = rebate;
}
public double acceptCash(double money){
return money*rebate;
}
}
class CashReturn extends CashSuper{
private double moneyCondition;
private double moneyReturn;
public CashReturn(double moneyCondition,double moneyReturn){
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
public double getMoneyCondition() {
return moneyCondition;
}
public void setMoneyCondition(double moneyCondition) {
this.moneyCondition = moneyCondition;
}
public double getMoneyReturn() {
return moneyReturn;
}
public void setMoneyReturn(double moneyReturn) {
this.moneyReturn = moneyReturn;
}
public double acceptCash(double money){
if(money > moneyCondition){
return money - Math.floor(money/moneyCondition)*moneyReturn;
}else{
return money;
}
}
}
class CashFactory{
public static CashSuper creatCash(String select){
CashSuper cash = new CashSuper();
Scanner in = new Scanner(System.in);
switch(select){
case"normal":
cash = new CashNormal();
break;
case"rebate":
System.out.print("please enter the rebate: ");
double rebat = in.nextDouble();
cash = new CashRebate(rebat);
break;
case"return":
System.out.print("please enter the moneyCondition: ");
double moneyCondition = in.nextDouble();
System.out.print("please enter the moneyReturn: ");
double moneyReturn = in.nextDouble();
cash = new CashReturn(moneyCondition,moneyReturn);
break;
}
in.close();
return cash;
}
}
class CashContext{
CashSuper cs =null;
Scanner in = new Scanner(System.in);
public CashContext(String type){
switch(type){
case"normal":
CashNormal cash0 = new CashNormal();
cs = cash0;
break;
case"rebate":
System.out.print("please enter the rebate: ");
double rebat = in.nextDouble();
CashRebate cash1 = new CashRebate(rebat);
cs = cash1;
break;
case"return":
System.out.print("please enter the moneyCondition: ");
double moneyCondition = in.nextDouble();
System.out.print("please enter the moneyReturn: ");
double moneyReturn = in.nextDouble();
CashReturn cash2 = new CashReturn(moneyCondition,moneyReturn);
cs = cash2;
break;
}
}
public double GetResult(double money){
return cs.acceptCash(money);
}
}
public class TestDemo{
public static void main(String[] args)throws Exception{
Scanner in = new Scanner(System.in);
System.out.print("please enter the price: ");
double price = in.nextDouble();
System.out.print("please enter the num: ");
int num = in.nextInt();
System.out.print("please enter the select: ");
String select = in.next();
CashContext csuper = new CashContext(select);
System.out.println(csuper.GetResult(num*price));
in.close();
}
}
策略模式:一种定义一系列算法的方法。所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
策略模式是用来封装算法的,只要在分析过程中听到需要再不同时间应用不同的业务规则,就可以考虑。