策略模式应对旅行中出现的问题

策略模式是针对在进行过程中出现一些问题的应对模式。一般可以分为2种 :按顺序给予问题应对方法或者根据出现的问题给予处理方法。个人觉着还是后面的比较实际一些,更能体现出随机应变。

 

策略模式分为3个部分

1.抽象策略类(通常是一个抽象类或者接口来声明策略具备的功能)

2.具体策略类(继承抽象策略类,实现具体的应对方法)

3.应用策略类(掌控策略的使用)

 

下面举例说明策略模式:

情景:一个导游要组织一次户外旅游,那么在户外旅游时候可能出现很多问题需要他来处理,这里只说简单的三种:食物不够、车子坏了、风景不好看三个问题。那么导游在出发以前已经预料到可能出现这些问题,他就在大脑中想出这三种问题的解决办法:买食物、修车子、换景区,在旅行当中应对这三种问题。

 

 

三个解决方案都是处理问题的

IStrategy

package j2se.pattern;

/**
 * 应对问题的操作
 * @author Lyon Yao
 *
 */
public interface IStrategy {
	/**
	 * 处理问题
	 */
	public void dealProblem();
}

 三种具体应对方案:

1.BuyMoreFood

 

package j2se.pattern.impl;

import j2se.pattern.IStrategy;

/**
 * 应对食物不足
 * @author Lyon Yao
 *
 */
public class BuyMoreFood implements IStrategy {

	@Override
	public void dealProblem() {
		// TODO Auto-generated method stub1
		System.out.println("Our food is not enough and we must find a store to buy more food!");
	}

}

 2.FixCar

 

package j2se.pattern.impl;

import j2se.pattern.IStrategy;

/**
 * 车子坏了
 * @author Lyon Yao
 *
 */
public class FixCar implements IStrategy {

	@Override
	public void dealProblem() {
		// TODO Auto-generated method stub
		System.out.println("there some wrong with our car,we must to repair it!");
	}

}
 

3.ToAnOtherPlace

 

package j2se.pattern.impl;

import j2se.pattern.IStrategy;

/**
 * 应对风景不好
 * @author Lyon Yao
 *
 */
public class ToAnOtherPlace implements IStrategy{

	@Override
	public void dealProblem() {
		// TODO Auto-generated method stub
		System.out.println("Scenery is good,go back early!");
	}

}

 领队

Leader

 

package j2se;

import j2se.pattern.IStrategy;
import j2se.pattern.impl.BuyMoreFood;
import j2se.pattern.impl.FixCar;
import j2se.pattern.impl.ToAnOtherPlace;

import java.util.HashMap;
import java.util.Map;

/**
 * 领队
 * @author Lyon Yao
 *
 */
public class Leader {
	/**
	 * 领队的大脑,存放很多处理方法
	 */
	private Map<String,IStrategy> brain=new HashMap<String, IStrategy>(0);
	private IStrategy cur_deal=null;
	/**
	 * 遇到问题操作
	 * @param problem
	 */
	public void operate(String problem){
		cur_deal=brain.get(problem);
		if(cur_deal!=null){
			cur_deal.dealProblem();
		}
	}
	/**
	 * 旅行之前考虑可能出现的问题,将应对方法放入大脑
	 */
	public void  thinkBeforeTravel(){
		IStrategy problem=new BuyMoreFood();
		brain.put("foodProlem", problem);
		problem=new ToAnOtherPlace();
		brain.put("SceneryProlem", problem);
		problem=new FixCar();
		brain.put("carProlem", problem);
	}
}

 测试类

 

package j2se;

import java.util.Random;

import org.junit.Before;
import org.junit.Test;

/**
 * 测试实例
 * @author Lyon Yao
 *
 */
public class TestCase {
	private Leader leader=new Leader();
	@Before
	public void beforeTravel(){
		leader.thinkBeforeTravel();
	}
	
	@Test
	public void travelTest(){
		String problems[]=new String[]{"foodProlem","SceneryProlem","carProlem"};
		Random ran=new Random();
		//不可预料的问题
		for(int i=0;i<10;i++){
			int problemIndex=ran.nextInt(3);
			String problem=problems[problemIndex];
			leader.operate(problem);
		}
		
	}
}

 执行结果:

 

there some wrong with our car,we must to repair it!

Scenery is good,go back early!

there some wrong with our car,we must to repair it!

Our food is not enough and we must find a store to buy more food!

there some wrong with our car,we must to repair it!

Scenery is good,go back early!

there some wrong with our car,we must to repair it!

Our food is not enough and we must find a store to buy more food!

Our food is not enough and we must find a store to buy more food!

Our food is not enough and we must find a store to buy more food!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值