策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
简单工厂模式实现
package 大话设计模式;
import java.util.Scanner;
public class Cash {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
String list = "";
double totalPrice = 0.00;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("输入单价:");
String price = sc.nextLine();
sc = new Scanner(System.in);
System.out.println("输入数量:");
String num = sc.nextLine();
sc = new Scanner(System.in);
System.out.println("输入折扣类型(1 正常收费 2 打折 3 满减)");
String type = sc.nextLine();
double discount = 0.0d;
double baseCash = 0;
double returnCash = 0;
if ("2".equals(type)) {
sc = new Scanner(System.in);
System.out.println("输入折扣:");
discount = Double.parseDouble(sc.nextLine());
}
if ("3".equals(type)) {
sc = new Scanner(System.in);
System.out.println("返现基础金额:");
baseCash = Double.parseDouble(sc.nextLine());
sc = new Scanner(System.in);
System.out.println("返现金额:");
returnCash = Double.parseDouble(sc.nextLine());
}
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
CashSuper cs = CashFactory.createCashAccept(type, discount, baseCash, returnCash);
double xiaoji = cs.acceptCash(xianjin);
list += "单价:" + price + "数量:" + num + ",小计:" + xiaoji + "\n";
totalPrice += xiaoji;
if (totalPrice > 10) {
flag = false;
}
}
System.out.println(list);
System.out.println("总价:" + totalPrice);
}
}
abstract class CashSuper {
// 参数为原价,返回位当前价
public abstract double acceptCash(double money);
}
// 正常收费子类
class CashNormal extends CashSuper {
// 正常收费,原价返回
public double acceptCash(double money) {
return money;
}
}
// 打折收费子类
class CashRebate extends CashSuper {
private double discount = 0.00;
// 打折收费,初始化时,必需要输入折扣率,如八折,就是0.8
public CashRebate(double discount) {
this.discount = discount / 10;
}
public double acceptCash(double money) {
return this.discount*money;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
}
// 返利收费子类
class CashReturn extends CashSuper {
private double baseCash;
private double returnCash;
// 返利收费,初始化时必需输入返利条件和返利值,比如满300返100,则baschCash300,returnCash为100
public CashReturn(double baseCash, double returnCash) {
this.baseCash = baseCash;
this.returnCash = returnCash;
}
public double acceptCash(double money) {
double result = money;
if (money >= baseCash)
result = money - Math.floor(money / baseCash) * returnCash;
return result;
}
}
// 现金收费工厂类
class CashFactory {
public static CashSuper createCashAccept(String type, double discount, double baseCash, double returnCash) {
CashSuper cs = null;
if ("1".equals(type))
cs = new CashNormal();
else if ("2".equals(type))
cs = new CashRebate(discount);
else if ("3".equals(type))
cs = new CashReturn(baseCash, returnCash);
return cs;
}
}
仅策略模式
package 大话设计模式;
import java.util.Scanner;
public class Cash {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
String list = "";
double totalPrice = 0.00;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("输入单价:");
String price = sc.nextLine();
sc = new Scanner(System.in);
System.out.println("输入数量:");
String num = sc.nextLine();
sc = new Scanner(System.in);
System.out.println("输入折扣类型(1 正常收费 2 打折 3 满减)");
String type = sc.nextLine();
double discount = 0.0d;
double baseCash = 0;
double returnCash = 0;
***
## CashSuper cs=null;
***
if("1".equals(type))cs=new CashNormal();
if ("2".equals(type)) {
sc = new Scanner(System.in);
System.out.println("输入折扣:");
discount = Double.parseDouble(sc.nextLine());
cs=new CashRebate(discount);
}
if ("3".equals(type)) {
sc = new Scanner(System.in);
System.out.println("返现基础金额:");
baseCash = Double.parseDouble(sc.nextLine());
sc = new Scanner(System.in);
System.out.println("返现金额:");
returnCash = Double.parseDouble(sc.nextLine());
cs=new CashReturn(baseCash,returnCash);
}
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
CashContext cc = new CashContext(cs);
//将相应的策略对象作为参数传入CashContext构造函数中
double xiaoji = cc.GetResult(xianjin);
list += "单价:" + price + "数量:" + num + ",小计:" + xiaoji + "\n";
totalPrice += xiaoji;
if (totalPrice > 10) {
flag = false;
}
}
System.out.println(list);
System.out.println("总价:" + totalPrice);
}
}
abstract class CashSuper {
// 参数为原价,返回位当前价
public abstract double acceptCash(double money);
}
// 正常收费子类
class CashNormal extends CashSuper {
// 正常收费,原价返回
public double acceptCash(double money) {
return money;
}
}
// 打折收费子类
class CashRebate extends CashSuper {
private double discount = 0.00;
// 打折收费,初始化时,必需要输入折扣率,如八折,就是0.8
public CashRebate(double discount) {
this.discount = discount / 10;
}
public double acceptCash(double money) {
return this.discount*money;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
}
// 返利收费子类
class CashReturn extends CashSuper {
private double baseCash;
private double returnCash;
// 返利收费,初始化时必需输入返利条件和返利值,比如满300返100,则baschCash300,returnCash为100
public CashReturn(double baseCash, double returnCash) {
this.baseCash = baseCash;
this.returnCash = returnCash;
}
public double acceptCash(double money) {
double result = money;
if (money >= baseCash)
result = money - Math.floor(money / baseCash) * returnCash;
return result;
}
}
// 现金收费工厂类
class CashFactory {
public static CashSuper createCashAccept(String type, double discount, double baseCash, double returnCash) {
CashSuper cs = null;
if ("1".equals(type))
cs = new CashNormal();
else if ("2".equals(type))
cs = new CashRebate(discount);
else if ("3".equals(type))
cs = new CashReturn(baseCash, returnCash);
return cs;
}
}
****class CashContext{
private CashSuper cs;//声明一个现金收费父类对象
public CashContext(CashSuper cs) {
//通过构造方法,传入具体的收费策略对象(正常,打折,返利,积分)
this.cs = cs;
}
public double GetResult(double money) {
//根据不同收费策略,获得合计结果
return cs.acceptCash(money);
}
}****
简单工厂+策略模式
package 大话设计模式;
import java.util.Scanner;
public class Cash {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
String list = "";
double totalPrice = 0.00;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("输入单价:");
String price = sc.nextLine();
sc = new Scanner(System.in);
System.out.println("输入数量:");
String num = sc.nextLine();
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
***CashContext cc = new CashContext();***
double xiaoji = cc.GetResult(xianjin);
list += "单价:" + price + "数量:" + num + ",小计:" + xiaoji + "\n";
totalPrice += xiaoji;
if (totalPrice > 10) {
flag = false;
}
}
System.out.println(list);
System.out.println("总价:" + totalPrice);
}
}
abstract class CashSuper {
// 参数为原价,返回位当前价
public abstract double acceptCash(double money);
}
// 正常收费子类
class CashNormal extends CashSuper {
// 正常收费,原价返回
public double acceptCash(double money) {
return money;
}
}
// 打折收费子类
class CashRebate extends CashSuper {
private double discount = 0.00;
// 打折收费,初始化时,必需要输入折扣率,如八折,就是0.8
public CashRebate(double discount) {
this.discount = discount / 10;
}
public double acceptCash(double money) {
return this.discount*money;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
}
// 返利收费子类
class CashReturn extends CashSuper {
private double baseCash;
private double returnCash;
// 返利收费,初始化时必需输入返利条件和返利值,比如满300返100,则baschCash300,returnCash为100
public CashReturn(double baseCash, double returnCash) {
this.baseCash = baseCash;
this.returnCash = returnCash;
}
public double acceptCash(double money) {
double result = money;
if (money >= baseCash)
result = money - Math.floor(money / baseCash) * returnCash;
return result;
}
}
// 现金收费工厂类
class CashFactory {
public static CashSuper createCashAccept(String type, double discount, double baseCash, double returnCash) {
CashSuper cs = null;
if ("1".equals(type))
cs = new CashNormal();
else if ("2".equals(type))
cs = new CashRebate(discount);
else if ("3".equals(type))
cs = new CashReturn(baseCash, returnCash);
return cs;
}
}
class CashContext{
private CashSuper cs=null;//声明一个现金收费父类对象
***public CashContext()*** {
Scanner sc = new Scanner(System.in);
System.out.println("输入折扣类型(1 正常收费 2 打折 3 满减)");
String type = sc.nextLine();
double discount = 0.0d;
double baseCash = 0;
double returnCash = 0;
if("1".equals(type))cs=new CashNormal();
if ("2".equals(type)) {
sc = new Scanner(System.in);
System.out.println("输入折扣:");
discount = Double.parseDouble(sc.nextLine());
cs=new CashRebate(discount);
}
if ("3".equals(type)) {
sc = new Scanner(System.in);
System.out.println("返现基础金额:");
baseCash = Double.parseDouble(sc.nextLine());
sc = new Scanner(System.in);
System.out.println("返现金额:");
returnCash = Double.parseDouble(sc.nextLine());
cs=new CashReturn(baseCash,returnCash);
}
}
public double GetResult(double money) {
//根据不同收费策略,获得合计结果
return cs.acceptCash(money);
}
}