感觉就是策略模式啊....
class SContext{
private Weather weather;
public void setWeather(Weather weather) {
this.weather = weather;
}
public Weather getWeather() {
return weather;
}
public String WeatherMessage(){
return weather.getWeather();
}
}
interface Weather{
String getWeather();
}
//concreteStatsubClass
class Rain implements Weather{
@Override
public String getWeather() {
return "rain";
}
}
class Sunshine implements Weather{
@Override
public String getWeather() {
return "sunshine";
}
}
public class StateTest {
public static void main(String[] args){
SContext context = new SContext();
context.setWeather(new Sunshine());
System.out.println(context.WeatherMessage());
SContext context1 = new SContext();
context1.setWeather(new Rain());
System.out.println(context1.WeatherMessage());
}
}
网友评论