设计模式之策略模式的实现
package Lab23_Strategy;
public class Travel {
public static class ArrayHandler { // 环境类 是使用算法的角色
private tourType type;
public void choice() {
type.choice();
}
public void setchoice(tourType choice) {
this.type = choice;
}
}
public interface tourType { // 算法接口类
public void choice();
}
public static class plane implements tourType { // 具体出行类
public void choice() {
System.out.println("飞机出行");
}
}
public static class train implements tourType { // 具体出行类
public void choice() {
System.out.println("火车出行");
}
}
public static class car implements tourType { // 具体出行类
public void choice() {
System.out.println("汽车出行");
}
}
public static void main(String args[]) {
ArrayHandler arrayHandler = new ArrayHandler();
tourType type = new plane();
arrayHandler.setchoice(type);
System.out.println("选择的出行方式为:");
arrayHandler.choice();
}
}