码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-17 23:51:53      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:ar   color   使用   sp   on   div   art   bs   代码   

定义:

当一个对象内在状态改变是允许改变其行为,这个对象看起来像是改变了其类。

状态模式的好处就是讲与特定状态相关的行为进行了局部化,并且将不同状态行为进行分割。也就是将特定的与改状态相关的行为都放进一个对象中,由于所有与状态有关的代码都存在于某个具体的状态类中,所以通过定义新的具体的状态类就可以很容易的增加新的状态和转换。

举个例子:火车总是根据时速不停的转换模式,是减速、加速还是匀速等。

火车类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Train {
    private StateTrain currentStateTrain;
    public Train(){
        currentStateTrain = new StartState();
    }
    private int speed;
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public void setCurrentStateTrain(StateTrain currentStateTrain) {
        this.currentStateTrain = currentStateTrain;
    }
    public StateTrain getCurrentStateTrain() {
        return currentStateTrain;
    }
    public int getSpeed() {
        return speed;
    }
    public void run(){
        currentStateTrain.run(this);
    }
}

状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public abstract class StateTrain {
    public abstract void run(Train train);
}
public class StartState extends StateTrain {
 
    @Override
    public void run(Train train) {
        if(train.getSpeed() == 0){
            System.out.println("start");
        }else {
            train.setCurrentStateTrain(new FasterState());
            train.run();
        }
    }
}
public class SlowState extends StateTrain {
 
    @Override
    public void run(Train train) {
        if(train.getSpeed() > 200){
            System.out.println("stop");
        }else {
            train.setCurrentStateTrain(new StopState());
            train.run();
        }
    }
 
}
public class FasterState extends StateTrain {
 
    @Override
    public void run(Train train) {
        if(train.getSpeed() < 200){
            System.out.println("faster");
        }else {
            train.setCurrentStateTrain(new SlowState());
            train.run();
        }
    }
 
}
public class StopState extends StateTrain {
 
    @Override
    public void run(Train train) {
        System.out.println("stop the train");
    }
 
}

这个例子说明当一个对象的行为取决于其状态,并且他必须在运行时根据状态的改变而改变行为时就可以考虑使用状态模式了。同样的,如果某项业务具有多个状态,而状态的变化要依赖于大量的分支判断语句来实现时,就应该考虑将每一个状态都定义为一个state子类,这样增加或删除一个状态时比较容易。

 

基本代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public abstract class State {
    public String stateName;
    public abstract void handle(Context context);
}
public class ConcreteStateA extends State {
 
    public ConcreteStateA(){
        stateName = "state A";
    }
    @Override
    public void handle(Context context) {
        context.setState(new ConcreteStateB());
    }
 
}
public class ConcreteStateB extends State {
 
    public ConcreteStateB() {
        stateName = "state B";
    }
    @Override
    public void handle(Context context) {
        context.setState(new ConcreteStateA());
    }
 
}

上下文代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Context {
    private State state;
    public Context(State state){
        this.state = state;
    }
    public void setState(State state) {
        System.out.println("now state is:"+this.state.stateName);
        this.state = state;
        System.out.println("state change is:"+this.state.stateName);
    }
    public void request(){
        state.handle(this);
    }
}

客户端:

1
2
3
4
5
public static void main(String args[]) {
        Context context = new Context(new ConcreteStateA());
        context.request();
        context.request();
    }

一个对象的状态指的就足这个对像的属性值的组合,如果我们改变了这个属性的任意一个,那么对象的状态就发生了改变,一般地。调用一个对象的方法都会改变这个对象的状态,因为这个方法往往会调用并改变自己的属性。

 

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

标签:ar   color   使用   sp   on   div   art   bs   代码   

原文地址:http://www.cnblogs.com/silence-hust/p/4170631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!