标签:
1 package com.shejimoshi.behavioral.Strategy; 2 3 4 /** 5 * 功能:上班的接口 6 * 时间:2016年3月9日下午8:53:34 7 * 作者:cutter_point 8 */ 9 public interface ToWork 10 { 11 /** 12 * 上班方式 13 */ 14 public abstract void workStyle(); 15 16 }
1 package com.shejimoshi.behavioral.Strategy; 2 3 4 /** 5 * 功能:走步上班 6 * 时间:2016年3月9日下午8:55:15 7 * 作者:cutter_point 8 */ 9 public class WalkingWork implements ToWork 10 { 11 12 @Override 13 public void workStyle() 14 { 15 System.out.println("走步上班"); 16 } 17 18 }
1 package com.shejimoshi.behavioral.Strategy; 2 3 4 /** 5 * 功能:使用工具去上班 6 * 时间:2016年3月9日下午8:57:35 7 * 作者:cutter_point 8 */ 9 public class ToolToWork implements ToWork 10 { 11 12 @Override 13 public void workStyle() 14 { 15 System.out.println("使用工具去上班"); 16 } 17 18 }
1 package com.shejimoshi.behavioral.Strategy; 2 3 4 /** 5 * 功能:选择方式 6 * 时间:2016年3月9日下午9:15:52 7 * 作者:cutter_point 8 */ 9 public class Select 10 { 11 private ToWork tw; 12 13 public Select(String type) 14 { 15 switch(type) 16 { 17 case "步行": 18 WalkingWork ww = new WalkingWork(); 19 tw = ww; 20 break; 21 case "使用工具": 22 ToolToWork ttw = new ToolToWork(); 23 tw = ttw; 24 break; 25 } 26 } 27 28 /** 29 * 执行相应的策略 30 */ 31 public void getResult() 32 { 33 tw.workStyle(); 34 } 35 }
1 package com.shejimoshi.behavioral.Strategy; 2 3 4 /** 5 * 功能:定义一系列的算法,把他们一个个封装起来,并且使他们可互相替换。 6 * 适用:许多相关的类仅仅是行为有差异 7 * 需要使用一个算法的不同变体 8 * 算法使用客户不该知道的数据 9 * 时间:2016年3月9日下午8:49:34 10 * 作者:cutter_point 11 */ 12 public class Test 13 { 14 public static void main(String[] args) 15 { 16 Select st = new Select("步行"); 17 st.getResult(); 18 Select st2 = new Select("使用工具"); 19 st2.getResult(); 20 } 21 }
走步上班 使用工具去上班
标签:
原文地址:http://www.cnblogs.com/cutter-point/p/5259874.html