《软件设计与体系结构课件11状态和策略模式》由会员分享,可在线阅读,更多相关《软件设计与体系结构课件11状态和策略模式(68页珍藏版)》请在金锄头文库上搜索。
1、软件设计与体系结构 状态和策略模式 主讲:李健利,状态模式,意图 =允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。 动机,状态模式(续),适用性 以下情况可使用状态模式 =对象的行为取决于其状态, 并且它必须在运行期根据该状态改变它的行为。 =一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。状态模式将每一个条件分支放入一个独立的类中,状态模式(续),结构,状态模式(续),状态模式,效果 =收益 将所有与一个状态相关的行为都放入一个对象中 允许状态转换逻辑结合到状态对象中而不是放入单块的if或switch 语句。 有助于避免不一致的状态,因为状态改变
2、的发生只使用一个状态对象,不是几个对象或属性 =缺陷 增加了对象的数量,状态模式例1,考虑有push() 和pull()这两个方法的类,这两个方法行为的改变依赖于对象的状态 为向对象发送推和拉请求,我们将使用具有“推”和“拉”按钮的下列GUI: 对象的状态将由GUI上部的墙纸颜色指明 状态包括:black、 red、blue 和green,状态模式例1 (续),首先,不使用状态模式来做: /* * Class ContextNoSP has behavior dependent on its state. * The push() and pull() methods do different
3、 things * depending on the state of the object. * This class does NOT use the State pattern. */ public class ContextNoSP / The state! private Color state = null; / Creates a new ContextNoSP with the specified state (color). public ContextNoSP(Color color) state = color; / Creates a new ContextNoSP w
4、ith the default state public ContextNoSP() this(Color.red);,状态模式例1 (续),/ Returns the state. public Color getState() return state; / Sets the state. public void setState(Color state) this.state = state; /* * The push() method performs different actions depending * on the state of the object. Actually
5、, right now * the only action is to make a state transition. */ public void push() if (state = Color.red) state = Color.blue; else if (state = Color.green) state = Color.black; else if (state = Color.black) state = Color.red; else if (state = Color.blue) state = Color.green; ,状态模式例1 (续),/* * The pul
6、l() method performs different actions depending * on the state of the object. Actually, right now * the only action is to make a state transition. */ public void pull() if (state = Color.red) state = Color.green; else if (state = Color.green) state = Color.blue; else if (state = Color.black) state =
7、 Color.green; else if (state = Color.blue) state = Color.red; ,状态模式例1 (续),以下是GUI测试程序的一部分: /* * Test program for the ContextNoSP class * which does NOT use the State pattern. */ public class TestNoSP extends Frame implements ActionListener / GUI attributes. private Button pushButton = new Button(“Pus
8、h Operation“); private Button pullButton = new Button(“Pull Operation“); private Button exitButton = new Button(“Exit“); private Canvas canvas = new Canvas(); / The Context. private ContextNoSP context = null;,状态模式例1 (续),public TestNoSP() super(“No State Pattern“); context = new ContextNoSP(); setup
9、Window(); private void setupWindow() / Setup GUI / Handle GUI actions. public void actionPerformed(ActionEvent event) Object src = event.getSource(); if (src = pushButton) context.push(); canvas.setBackground(context.getState(); ,状态模式例1 (续),else if (src = pullButton) context.pull(); canvas.setBackgr
10、ound(context.getState(); else if (src = exitButton) System.exit(0); / Main method. public static void main(String argv) TestNoSP gui = new TestNoSP(); gui.setVisible(true); ,状态模式例1 (续),现在使用状态模式! 其类图如下:,状态模式例1 (续),首先将定义抽象State 类: /* * Abstract class which defines the interface for the * behavior of a
11、 particular state of the Context. */ public abstract class State public abstract void handlePush(Context c); public abstract void handlePull(Context c); public abstract Color getColor(); 然后将为不同的状态:RedState, BlackState, BlueState 和GreenState,编写具体的State 类,状态模式例1 (续),例如:下面的BlackState类: public class Bla
12、ckState extends State / Next state for the Black state: / On a push(), go to “red“ / On a pull(), go to “green“ public void handlePush(Context c) c.setState(new RedState(); public void handlePull(Context c) c.setState(new GreenState(); public Color getColor() return (Color.black); ,状态模式例1 (续),下面是使用状
13、态模式和State类的新Context类: /* * Class Context has behavior dependent on its state. * This class uses the State pattern. * Now when we get a pull() or push() request, we * delegate the behavior to our contained state object! */ public class Context / The contained state. private State state = null; / Stat
14、e attribute / Creates a new Context with the specified state. public Context(State state) this.state = state;,状态模式例1 (续),/ Creates a new Context with the default state. public Context() this(new RedState(); / Returns the state. public State getState() return state; / Sets the state. public void setS
15、tate(State state) this.state = state;,状态模式例1 (续),/* * The push() method performs different actions depending * on the state of the object. Using the State pattern, * we delegate this behavior to our contained state object. */ public void push() state.handlePush(this); /* * The pull() method performs
16、 different actions depending * on the state of the object. Using the State pattern, * we delegate this behavior to our contained state object. */ public void pull() state.handlePull(this); ,状态模式,实现问题 =谁定义状态转换? Context类 = 适用于简单情形 ConcreteState类 = 通常更灵活,但造成ConcreteState 之间的实现依赖 例1让ConcreteState类定义状态转换 =何时创建ConcreteState 对象? 按需创建ConcreteState 对象 一次性创建所有的ConcreteState