策略模式

策略模式

为什么要使用策略模式?

解答:当一个接口中有三种判断,参数传入month月,是一种结果,传入year是一种结果,传入day又是另一种结果,如果使用传统的代码业务逻辑如下:

if(x == month){
	System.out.println("-----返回月的数据----");
}else if(x == year){
	System.out.println("-----返回年的数据----");
}else if(x == day){
	System.out.println("-----返回天的数据---");
}

使用大量的if else会很难维护,这时候如果使用策略模式就非常方便。

什么是策略模式?

解答:1.诸葛亮的锦囊妙计,每一个锦囊都是一个策略。2.如上面的判断,每一种结果都是一个策略。

怎么使用?

解答:将上面的年月日的判断封装成一个Judge接口,然后新建三个类实现MonthJudge,YearJudge,DayJudge。

代码演示:

1.新建一个judge接口,里面只有一个方法,判断方法

public interface Judge {
	void judge();
}

2.新建三个类MonthJudge,YearJudge,DayJudge 实现 judge接口

public class MonthJudge implements Judge{
	@Override
	public void judge() {
		System.out.println("这里是月份判断的结果");
	}
}
public class YearJudge implements Judge{
	@Override
	public void judge() {
		System.out.println("这里是年份进来的结果");
	}
}
public class DayJudge implements Judge{
	@Override
	public void judge() {
		System.out.println("这里是日子的结果");
	}
}

3.建立一个实体类

public class DateClass {
	Judge judge;

	public DateClass(Judge judge) {
		this.judge = judge;
	}
	public void judge(){
		if(judge !=null){
			judge.judge();
		}
	}
}

4.这里就是判断,可以使用demo的main方法

public class Demo {
	public static void main(String[] args) {
		//1.如果前端传递的是month条件,那就调用month,如果是day,那就使用new DayJudge().
		DateClass dateClass = new DateClass(new MonthJudge());
		dateClass.judge();
	}
}

------------以上就是自己总结的,类图如下-------------
策略模式_第1张图片
如果大家觉得太简单,那接下来,咱们在来一个

案例2:商品促销行为:购物立减,购物满减,购物返现,无促销活动

思路:1.首先建促销的接口(Promotion),然后建4个策略(ManjianPromotion满减),(FanxianPromotion返现),(LijianPromotion立减),(NoPromotion无任何活动)

2.建立一个类PromotionActivity(行为)里面有执行行为,构造器里也是促销的接口

3.demo测试

代码如下:

//促销的接口
public interface Promotion {
	public void doPromotion();
}
//返现
public class FanxianPromotion implements Promotion{
	@Override
	public void doPromotion() {
		System.out.println("购物满100返现20元,返现金额存入商户中");
	}
}
//立减
public class LijianPromotion implements Promotion{
	@Override
	public void doPromotion() {
		System.out.println("立减促销,购物立减200元");
	}
}
//满减
public class ManjianPromotion implements Promotion{
	@Override
	public void doPromotion() {
		System.out.println("购物满减促销,满1000-200元");
	}
}
//无活动
public class NoPromotion implements Promotion {
	@Override
	public void doPromotion() {
		System.out.println("无任何活动");
	}
}
//行为类
public class PromotionActivity {

	private Promotion promotion;

	public PromotionActivity(Promotion promotion) {
		this.promotion = promotion;
	}
	public void executePromotion(){
		promotion.doPromotion();
	}
}

demo测试类:第一种情况

public class Test {
	public static void main(String[] args) {
		//618 活动,立减策略
		PromotionActivity promotionActivity618 = new PromotionActivity(new LijianPromotion());
		//双十一 返现
		PromotionActivity promotionActivity1111 = new PromotionActivity(new FanxianPromotion());

		promotionActivity618.executePromotion();
		promotionActivity1111.executePromotion();
	}
}

demo测试类:第二种情况(如果是前端传递参数为LIJIAN或者FANXIAN)

就只能用if else了

//main方法
public static void main(String[] args) {
		PromotionActivity promotionActivity = null;
		String promotionKey = "LIJIAN";
		if(promotionKey.equals("LIJIAN")){
			//618 活动,立减策略
			 promotionActivity = new PromotionActivity(new LijianPromotion());
		}else if(promotionKey.equals("FANXIAN")){
			//双十一 返现
			 promotionActivity = new PromotionActivity(new FanxianPromotion());
		}
		promotionActivity.executePromotion();
	}

当然机智的你,肯定不想要这种if else判断了,如果用这种判断,那学了那么多的策略模式还有何意义。这时候咱们可以使用策略模式+工厂模式组合来搞。

//策略工厂类
public class PromotionActivityFactory {
	private static Map promotionMap = new HashMap<>();

	static {
		promotionMap.put(PromotionKey.LIJIAN,new LijianPromotion());
		promotionMap.put(PromotionKey.FANXIAN,new FanxianPromotion());
		promotionMap.put(PromotionKey.MANJIAN,new ManjianPromotion());
	}
	private static final Promotion no_promotion = new NoPromotion(); //无活动策略
	private PromotionActivityFactory(){
	}

	public static Promotion getPromotion(String promotionKey){
		Promotion promotion = promotionMap.get(promotionKey);
		return promotion == null ? no_promotion : promotion;
	}
	private interface PromotionKey{
		String LIJIAN = "LIJIAN";
		String FANXIAN = "FANXIAN";
		String MANJIAN = "MANJIAN";
	}
}

测试方法如下:

public static void main(String[] args) {
		String promotionKey="LIJIAN";
		PromotionActivity promotionActivity = new PromotionActivity(PromotionActivityFactory.getPromotion(promotionKey));
		//双十一 返现
		promotionActivity.executePromotion();
	}

完美搞定!UML图如下:

策略模式_第2张图片

你可能感兴趣的:(java,设计模式)