策略模式和策略模式与简易工厂模式的结合

       今天抽时间看来一下大话数据模式中的策略模式,与前两天学的简易工厂的不同之处在于,简易工厂模式只是单纯的创建出算法对象,而具体的对算法的组合操作需要调用类进行组合,换而言之!策略模式呢,就是在算法和调用类之间增加了一层,算法-->算法的操作组合-->调用类。考虑到降低耦合性的目的,策略模式将对算法的组合操作单独的抽取了出来,作为结果类返回给调用类

       以大话设计模式中的案例来写代码(案例是商场收费系统,收费模式有原价,打折,满减返利三中),代码如下

//算法父类
package shopCharge;

public interface ChargeFather {
	public abstract double acceptCash(double money); 
}










//原价
package shopCharge;

public class CashNormal implements ChargeFather{

	@Override
	public double acceptCash(double money) {
		
		return money;
	}
	
}














//打折
package shopCharge;

public class CashRebate implements ChargeFather{
	private double moneyRebate;
	public CashRebate(double moneyRebate) {
		this.moneyRebate = moneyRebate;
	}
	@Override
	public double acceptCash(double money) {
		// TODO Auto-generated method stub
		return money*moneyRebate;
	}

}





//满减
package shopCharge;

public class CashReturn implements ChargeFather{
	private double moneyCondition;
	private double moneyReturn;
	public CashReturn(double moneyCondition,double moneyReturn) {
		this.moneyCondition = moneyCondition;
		this.moneyReturn = moneyReturn;
	}
	@Override
	public double acceptCash(double money) {
		if(money>moneyCondition) {
			return (money - (money/moneyCondition)*moneyReturn);
		}
		return money;
	}

}









//策略模式Context类(调用算法进行组合计算等操作)
package shopCharge;

public class CashContext {
	private ChargeFather cf;
	public CashContext(ChargeFather cf) {
		this.cf = cf;
	}
	public double getresult(double money) {
		return cf.acceptCash(money);
	}
}






//main函数
package shopCharge;

import javax.swing.text.AbstractDocument.Content;

public class ShopMain {

	public static void main(String[] args) {
		
		//模拟接收单价和商品数量
		double money = 100;
		double count = 2;
		double totle = money*2;
		String style = "满减";
		switch (style) {
		case "原价":
			CashContext ct = new CashContext(new CashNormal());
			double result = ct.getresult(totle);
			System.out.println("原价:"+result);
			break;
		case "打折":
			double rebate = 0.7;
			CashContext ct1 = new CashContext(new CashRebate(rebate));
			double result1 = ct1.getresult(totle);
			System.out.println("打七折:"+result1);
			break;
		case "满减":
			CashContext ct2 = new CashContext(new CashReturn(50, 4));
			double result2 = ct2.getresult(totle);
			System.out.println("满50减4:"+result2);
			break;
		default:
			break;
		}
		
	}

}

//

       刚开始看到这里的时候我有点疑惑,策略模式跟简易工厂模式有什么区别呢?假设商家打折和满减活动一起举办,只需要在context中增加一个算法方法,然后进行组合,直接返回给main函数,main函数只需要添加一个cash进行判断输出即可。

       做到这里会发现,main函数还需要判断输出,这不是回到老路子了吗,所以可以尝试着工厂模式和策略模式组合,代码如下:

//策略模式 Context类
package shopCharge;

public class CashContext {
	private ChargeFather cf = null;
	public CashContext(String style) {
		
		switch (style) {
		case "原价":
			CashNormal cn = new CashNormal();
			cf = cn;
			break;
		case "打7折":
			CashRebate ct = new CashRebate(0.7);
			cf = ct;
			break;
		case "满50减4":
			CashReturn cr = new CashReturn(50, 4);
			cf = cr;
			break;
		default:
			break;
		}
	}
	public double getresult(double money) {
		return cf.acceptCash(money);
	}
}









//main类
package shopCharge;

import javax.swing.text.AbstractDocument.Content;

public class ShopMain {

	public static void main(String[] args) {
		
		//模拟接收单价和商品数量
		double money = 100;
		double count = 2;
		double totle = money*2;
		String style = "满50减4";
		CashContext ct = new CashContext(style);
		double resule = ct.getresult(money);
		
	}

}

到此,策略模式基本完成,需要别的打折折扣或者满减,直接在context中的新加cash即可。

你可能感兴趣的:(笔记)