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

设计模式-?状态模式

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

相关文章

  • 设计模式-状态模式

    设计模式-状态模式 设计模式 状态模式的关键是区分事物内部的状态

  • 设计模式——状态模式

    设计模式——状态模式 在状态模式中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。 优点: 减少...

  • 设计模式-状态设计模式

    1.定义 对于某个操作,由于其状态的不同,表现出的行为会不同(如遥控器进行音量增加的操作,在电视是开机状态下是可以...

  • 设计模式——状态模式

    前言 设计模式是指导一个程序猿以更好的姿态处理一些问题,而不再像刚学编程的我们,只会使用if-else分支语句,或...

  • 设计模式--状态模式

    基本常识:策略模式与状态模式是双胞胎,在出生时才分开。 假设公司有个糖果机,1当糖果机由糖果,投入25分钱,转动曲...

  • 设计模式——状态模式

    在阎宏博士的《JAVA与模式》一书中开头是这样描述状态(State)模式的:状态模式,又称状态对象模式(Patte...

  • 设计模式《状态模式》

    引言   上一节我们说了策略模式。这一节我们讲讲策略模式的双胞胎弟弟:状态模式,这个模式大家可能不常见,也不常用,...

  • 设计模式——状态模式

    定义 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行...

  • 设计模式 - 状态模式

    模式定义 允许一个对象在其内部状态发生改变时改变它的行为。对象看起来似乎修改了它的类。 状态模式(State Pa...

  • 设计模式 ——— 状态模式

    STATE(状态) ———— 对象行为型模式 意图 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改...

网友评论

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

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