状态模式之星际应用

来源:互联网 发布:淘宝买枪零件犯法吗 编辑:程序博客网 时间:2024/04/30 16:09
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

一个对象有多种状态,在不同的状态下,同一种方法有不同的行为。如果用swich-case语句,将有大量的条件分支和逻辑代码混在一起。状态模式将每个状态封装到一个独立的类中,利用多态性使得不同状态下同一种方法表现不同的行为。

状态模式的UML图如下:

星际中人族的机枪兵Marine有两种状态:普通状态和打了兴奋针后的状态,两种状态下机枪兵的开枪频率是不同的,我们用状态模式来实现机枪兵的fire()方法。

 

首先定义抽象状态State接口,这个接口指定了机枪兵的fire行为:

public interface State {
    public void fire();
}

State接口有一个fire()方法,我们实现两个子类NormalStateExcitedState,分别表示普通状态和打了兴奋针后的状态,并实现具体的fire方法:

public class NormalState implements State {
    public void fire() {
       System.out.println("普通状态每秒开枪1次。");
    }
}
public class ExcitedState implements State {
    public void fire() {
       System.out.println("兴奋状态每秒开枪2次。");
    }
}

最后,定义机枪兵类Marine,每个Marine的实例代表一个机枪兵:

public class Marine {
    // 保持一个状态类的实例:
    private State state = new NormalState();
 
    // 为机枪兵设置状态:
    public void setState(State state) {
       this.state = state;
    }
 
    // fire()方法,实际调用的是state变量的fire()方法:
    public void fire() {
       state.fire();
    }
}

最后我们看看如何在客户端控制一个机枪兵的状态:

public static void main(String[] args) {
    // 创建一个机枪兵的实例:
    Marine marine = new Marine();
    // 调用fire()方法:
    marine.fire();
    // 设置为兴奋状态:
    marine.setState(new ExcitedState());
    // 再调用fire()方法:
    marine.fire();
}

对同一个Marine对象调用两次fire()方法,屏幕输出为:

普通状态每秒开枪1次。
兴奋状态每秒开枪2次。

可见机枪兵在两种状态下的同一个fire()方法有不同的行为。

使用状态模式的好处是每个状态被封装到一个独立的类中,这些类可以独立变化,而主对象中没有繁琐的swich-case语句,并且添加新的状态非常容易,只需要从State派生一个新类即可。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击