策略模式是一种定义一些列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方法调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象。这本身并没有解除客户端需要选择判断的压力,可以将策略模式和简单工厂模式结合,选择具体来实现的职责就可以给Context来承担,最大化地减轻了客户端的职责。
例子:商场收银软件
原始代码:
package Stragety;
import java.util.Scanner;
public class Stragety {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cash cash=new Cash();
boolean flag =true;
while(flag) {
cash.buttonOK();
System.out.println("------------------------------");
System.out.println("清单:\n"+cash.list);
System.out.println("总价:"+cash.toalprice);
if(cash.toalprice>10) {
flag=false;
}
}
}
}
class Cash{
public String list="";
public double toalprice=0.00;
public void buttonOK() {
Scanner sc=new Scanner(System.in);
System.out.println("输入单价:");
String price =sc.nextLine();
System.out.println("输入数量:");
String num =sc.nextLine();
double xiaoji =Double.parseDouble(price)*Integer.parseInt(num);
list+="单价:"+price+",数量:"+num+",小计:"+xiaoji+"\n";
toalprice+=xiaoji;
}
}
增加打折:
package Stragety;
import java.util.Scanner;
public class Stragety {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cash cash=new Cash();
boolean flag =true;
while(flag) {
cash.buttonOK();
System.out.println("------------------------------");
System.out.println("清单:\n"+cash.list);
System.out.println("总价:"+cash.toalprice);
if(cash.toalprice>10) {
flag=false;
}
}
}
}
class Cash{
public String list="";
public double toalprice=0.00;
public void buttonOK() {
Scanner sc=new Scanner(System.in);
System.out.println("输入单价:");
String price =sc.nextLine();
System.out.println("输入数量:");
String num =sc.nextLine();
System.out.println("输入折扣:");
String discount=sc.nextLine();
double xiaoji =Double.parseDouble(price)*Integer.parseInt(num)*Double.parseDouble(discount)/10;;
list+="单价:"+price+",数量:"+num+",小计:"+xiaoji+"\n";
toalprice+=xiaoji;
}
}
简单工厂实现:
package Stragety;
import java.util.Scanner;
public class Stragety1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
String list = "";
double toalprice = 0.00;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("输入单价:");
String price = sc.nextLine();
System.out.println("输入数量:");
String num = sc.nextLine();
System.out.println("输入折扣类型(1、无折扣 2、打折 3、满减)");
int type = sc.nextInt();
double discount = 10;
double baseprice = 0;
double returnprice = 0;
if (type == 2) {
sc = new Scanner(System.in);
System.out.println("输入折扣");
discount = Double.parseDouble(sc.nextLine());
}
else if (type == 3) {
sc = new Scanner(System.in);
System.out.println("返现基础金额");
baseprice = Double.parseDouble(sc.nextLine());
System.out.println("返现金额");
returnprice = Double.parseDouble(sc.nextLine());
}
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
CashSuper cs = CashAcceptFactory.createCashAccept(type, discount, baseprice, returnprice);
double xiaoji = cs.acceptCash(xianjin);
toalprice+=xiaoji;
list = "单价:" + price + ",数量:" + num + ",折扣:" + discount + ",小计:" + xiaoji + "\n";
System.out.println("------------------------------");
System.out.println("清单:\n" + list);
System.out.println("总价:" + toalprice);
if (toalprice > 10) {
flag = false;
}
}
}
}
//现金收取工厂
class CashAcceptFactory {
// 根据条件生成相应的对象
public static CashSuper createCashAccept(int type, double discount, double baseprice, double returnprice) {
CashSuper cs = null;
switch (type) {
case 1:
cs = new CashNormal();
break;
case 2:
cs = new CashRebate(discount);
break;
case 3:
cs = new CashReturn(baseprice, returnprice);
break;
}
return cs;
}
}
//现金收取父类
abstract class CashSuper {
// 抽象方法:收取现金,参数为原价,返回为当前价
public abstract double acceptCash(double money);
}
//正常收费类
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
//打折收费类,继承CashSuper
class CashRebate extends CashSuper {
private double discount = 0.00;
// 初始化时,必需要输入折扣率
public CashRebate(double discount) {
this.discount = discount / 10;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return this.discount * money;
}
public double getDiscount() {
return this.discount;
}
public void setDiscount(double discount) {
this.discount = discount / 10;
}
}
//返利收费,继承CashSuper
class CashReturn extends CashSuper {
private double basecash;// 基础金额
private double returncash;// 返现金额
// 初始化时必须要输入返利条件和返利值
public CashReturn(double basecash, double returncash) {
this.basecash = basecash;
this.returncash = returncash;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= basecash)// 若大于返利条件,则需要减去返利值
{
result = money - Math.floor(money / basecash) * returncash;
}
return result;
}
public double getBasecash() {
return basecash;
}
public void setBasecash(double basecash) {
this.basecash = basecash;
}
public double getReturncash() {
return returncash;
}
public void setReturncash(double returncash) {
this.returncash = returncash;
}
}
改造现金工厂:
package Stragety;
import java.util.Scanner;
public class Stragety2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag = true;
String list = "";
double toalprice = 0.00;
while (flag) {
Scanner sc = new Scanner(System.in);
System.out.println("输入单价:");
String price = sc.nextLine();
System.out.println("输入数量:");
String num = sc.nextLine();
System.out.println("输入折扣类型(1、无折扣 2、打折 3、满减)");
int type = sc.nextInt();
double discount = 10;
double baseprice = 0;
double returnprice = 0;
CashSuper cs=null;
if(type==1) {cs=new CashNormal();}
else if(type == 2) {
sc = new Scanner(System.in);
System.out.println("输入折扣");
discount = Double.parseDouble(sc.nextLine());
cs=new CashRebate(discount);
}
else if (type == 3) {
sc = new Scanner(System.in);
System.out.println("返现基础金额");
baseprice = Double.parseDouble(sc.nextLine());
System.out.println("返现金额");
returnprice = Double.parseDouble(sc.nextLine());
cs=new CashReturn(baseprice, returnprice);
}
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
CashContext cc=new CashContext(cs);
double xiaoji = cc.GetResult(xianjin);
toalprice+=xiaoji;
list = "单价:" + price + ",数量:" + num + ",折扣:" + discount + ",小计:" + xiaoji + "\n";
System.out.println("------------------------------");
System.out.println("清单:\n" + list);
System.out.println("总价:" + toalprice);
if (toalprice > 10) {
flag = false;
}
}
}
}
//现金收取控制类
class CashContext{
private CashSuper cs;//声明一个现金收取父类对象
public CashContext(CashSuper cs)
{
//通过够着方法,传入具体的收费对策
this.cs=cs;
}
public double GetResult(double money) {
//返回结果
return cs.acceptCash(money);
}
}
//现金收取父类
abstract class CashSuper {
// 抽象方法:收取现金,参数为原价,返回为当前价
public abstract double acceptCash(double money);
}
//正常收费类
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
//打折收费类,继承CashSuper
class CashRebate extends CashSuper {
private double discount = 0.00;
// 初始化时,必需要输入折扣率
public CashRebate(double discount) {
this.discount = discount / 10;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return this.discount * money;
}
public double getDiscount() {
return this.discount;
}
public void setDiscount(double discount) {
this.discount = discount / 10;
}
}
//返利收费,继承CashSuper
class CashReturn extends CashSuper {
private double basecash;// 基础金额
private double returncash;// 返现金额
// 初始化时必须要输入返利条件和返利值
public CashReturn(double basecash, double returncash) {
this.basecash = basecash;
this.returncash = returncash;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= basecash)// 若大于返利条件,则需要减去返利值
{
result = money - Math.floor(money / basecash) * returncash;
}
return result;
}
public double getBasecash() {
return basecash;
}
public void setBasecash(double basecash) {
this.basecash = basecash;
}
public double getReturncash() {
return returncash;
}
public void setReturncash(double returncash) {
this.returncash = returncash;
}
}
策略模式:
package Stragety;
public class Strategy3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Context con;
con=new Context(new CStrategy1());
con.contextInterface();
con=new Context(new CStrategy2());
con.contextInterface();
con=new Context(new CStrategy3());
con.contextInterface();
}
}
//定义所有支持算法的公共接口
abstract class Strategy{
public abstract void algorithInterace();
}
//CStrategy 封装了具体的算法或行为,继承于Strategy
class CStrategy1 extends Strategy
{
@Override
public void algorithInterace() {
// TODO Auto-generated method stub
System.out.println("算法一实现");
}
}
class CStrategy2 extends Strategy
{
@Override
public void algorithInterace() {
// TODO Auto-generated method stub
System.out.println("算法二实现");
}
}
class CStrategy3 extends Strategy
{
@Override
public void algorithInterace() {
// TODO Auto-generated method stub
System.out.println("算法三实现");
}
}
//Context 维护对一个Strategy对象的引用
class Context
{
Strategy sta;
public Context(Strategy sta) {
//初始化时,传入具体的策略对像
this.sta=sta;
}
public void contextInterface()
{
sta.algorithInterace();
//根据具体策略对象,调用其方法
}
}
简单工厂和策略模式相结合:
package Stragety;
import java.util.Scanner;
public class Stragety3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
boolean flag = true;
String list = "";
double toalprice = 0.00;
while (flag) {
System.out.println("输入单价:");
String price = sc.nextLine();
System.out.println("输入数量:");
String num = sc.nextLine();
double xianjin = Double.parseDouble(price) * Integer.parseInt(num);
CashContext cc=new CashContext();
double xiaoji = cc.GetResult(xianjin);
toalprice+=xiaoji;
list = "单价:" + price + ",数量:" + num + ",折扣:" +cc.discount + ",小计:" + xiaoji + "\n";
System.out.println("------------------------------");
System.out.println("清单:\n" + list);
System.out.println("总价:" + toalprice);
if (toalprice > 10) {
flag = false;
}
}
}
}
//现金收取控制类
class CashContext{
Scanner sc = new Scanner(System.in);
double discount = 10;
double baseprice = 0;
double returnprice = 0;
private CashSuper cs=null;//声明一个现金收取父类对象
public CashContext() {
System.out.println("输入折扣类型(1、无折扣 2、打折 3、满减)");
int type = sc.nextInt();
if(type==1) {
cs=new CashNormal();
}
else if(type==2) {
sc = new Scanner(System.in);
System.out.println("输入折扣");
discount = Double.parseDouble(sc.nextLine());
cs=new CashRebate(discount);
}
else if (type == 3) {
sc = new Scanner(System.in);
System.out.println("返现基础金额");
baseprice = Double.parseDouble(sc.nextLine());
System.out.println("返现金额");
returnprice = Double.parseDouble(sc.nextLine());
cs=new CashReturn(baseprice, returnprice);
}
}
public double GetResult(double money) {
return cs.acceptCash(money);
}
}
//现金收取父类
abstract class CashSuper {
// 抽象方法:收取现金,参数为原价,返回为当前价
public abstract double acceptCash(double money);
}
//正常收费类
class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return money;
}
}
//打折收费类,继承CashSuper
class CashRebate extends CashSuper {
private double discount = 0.00;
// 初始化时,必需要输入折扣率
public CashRebate(double discount) {
this.discount = discount / 10;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
return this.discount * money;
}
public double getDiscount() {
return this.discount;
}
public void setDiscount(double discount) {
this.discount = discount / 10;
}
}
//返利收费,继承CashSuper
class CashReturn extends CashSuper {
private double basecash;// 基础金额
private double returncash;// 返现金额
// 初始化时必须要输入返利条件和返利值
public CashReturn(double basecash, double returncash) {
this.basecash = basecash;
this.returncash = returncash;
}
@Override
public double acceptCash(double money) {
// TODO Auto-generated method stub
double result = money;
if (money >= basecash)// 若大于返利条件,则需要减去返利值
{
result = money - Math.floor(money / basecash) * returncash;
}
return result;
}
public double getBasecash() {
return basecash;
}
public void setBasecash(double basecash) {
this.basecash = basecash;
}
public double getReturncash() {
return returncash;
}
public void setReturncash(double returncash) {
this.returncash = returncash;
}
}
适用的场景?
- 1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
- 2、一个系统需要动态地在几种算法中选择一种。
- 3、如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。
策略模式的优点?
- 1、策略模式的Strategy类层次为Context定义了一系列的可供重用的算法或行为。继承有助于析取出这些算法中的公共功能。
- 2、简化了单元测试,因此每个算法都有自己的类,可以通过自己的接口单独测试。