美文网首页
设计模式-?状态模式

设计模式-?状态模式

作者: IAmWhoAmI | 来源:发表于2016-07-28 09:38 被阅读7次

    感觉就是策略模式啊....

    
    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());
        }
    }
    

    相关文章

      网友评论

          本文标题:设计模式-?状态模式

          本文链接:https://www.haomeiwen.com/subject/weoojttx.html