if ("轿车".equals(carType)) {
System.out.println("轿车");
} else if ("卡车".equals(carType)) {
System.out.println("卡车");
} else if ("suv".equals(carType)) {
System.out.println("SUV");
}
//车接口
public interface ICarService {
void showCar();
}
/**
**策略模式
*/
//轿车实现类
public class JiaoCheServiceImpl implements ICarService {
@Override
public void showCar() {
System.out.println("展示轿车");
}
}
//卡车实现类
public class KaCheServiceImpl implements ICarService {
@Override
public void showCar() {
System.out.println("卡车");
}
}
//suv实现类
public class SuvServiceImpl implements ICarService {
@Override
public void showCar() {
System.out.println("suv");
}
}
//服务工产类
public class CarServicesFactory {
private static final Map<String, ICarService> map = new HashMap<>();
static {
map.put("轿车", new JiaoCheServiceImpl());
map.put("卡车", new KaCheServiceImpl());
map.put("SUV", new SuvServiceImpl());
}
public static ICarService getCarService(String CarType) {
return map.get(CarType);
}
}
public class Test {
public static void main(String[] args) {
String carType = "轿车";
ICarService carService = CarServicesFactory.getCarService(carType);
carService.showCar();
}
}