首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-08-26 
策略模式应对旅行中出现的问题策略模式是针对在进行过程中出现一些问题的应对模式。一般可以分为2种 :按顺

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

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

下面举例说明策略模式:

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

?

?

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

IStrategy

package j2se.pattern;

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();@Beforepublic void beforeTravel(){leader.thinkBeforeTravel();}@Testpublic 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!


热点排行