- 浏览: 531339 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (231)
- 一个操作系统的实现 (20)
- 汇编(NASM) (12)
- Linux编程 (11)
- 项目管理 (4)
- 计算机网络 (8)
- 设计模式(抽象&封装) (17)
- 数据结构和算法 (32)
- java基础 (6)
- UML细节 (2)
- C/C++ (31)
- Windows (2)
- 乱七八糟 (13)
- MyLaB (6)
- 系统程序员-成长计划 (8)
- POJ部分题目 (10)
- 数学 (6)
- 分布式 & 云计算 (2)
- python (13)
- 面试 (1)
- 链接、装载与库 (11)
- java并行编程 (3)
- 数据库 (0)
- 体系结构 (3)
- C++ template / STL (4)
- Linux环境和脚本 (6)
最新评论
-
chuanwang66:
默默水塘 写道typedef void(*Fun)(void) ...
C++虚函数表(转) -
默默水塘:
typedef void(*Fun)(void);
C++虚函数表(转) -
lishaoqingmn:
写的很好,例子简单明了,将观察者模式都表达了出来。
这里是ja ...
观察者模式——Observer
1. 概述
The intent of the STATE pattern is to distribute state-specific logic across classes that represent an object’s state.
STATE 设计模式的目的 是:将特定状态相关的逻辑分散到一些类的状态类中。
2. 实例
旋转门: Consider the model of the state of a carousel door(revolving door). A carousel is a large, smart rack that accepts material through a doorway and stores the material according to a bar code ID on it. The door operates with a single button. See the state diagram below for some detail.
旋转门的状态图:(状态图细节见 4. )
3. 状态模型的两种实现方法
3.1 方法一: switch
Observable是java.util中的类^^居然以前都不晓得哦,该打!
Door的具体实现如下:
import java.util.Observable; /** * This class provides an initial model of a carousel door * that manages its state without moving state-specific * logic out to state classes. */ public class Door extends Observable { public final int CLOSED = -1; public final int OPENING = -2; public final int OPEN = -3; public final int CLOSING = -4; public final int STAYOPEN = -5; private int state = CLOSED; /** * The carousel user has touched the carousel button. This "one touch" * button elicits different behaviors, depending on the state of the door. */ public void touch() { switch (state) { case OPENING: case STAYOPEN: setState(CLOSING); break; case CLOSING: case CLOSED: setState(OPENING); break; case OPEN: setState(STAYOPEN); break; default: throw new Error("can't happen"); } } /** * This is a notification from the mechanical carousel that * the door finished opening or shutting. */ public void complete() { if (state == OPENING) setState(OPEN); else if (state == CLOSING) setState(CLOSED); } /** * This is a notification from the mechanical carousel that the * door got tired of being open. */ public void timeout() { setState(CLOSING); } /** * @return a textual description of the door's state */ public String status() { switch (state) { case OPENING: return "Opening"; case OPEN: return "Open"; case CLOSING: return "Closing"; case STAYOPEN: return "StayOpen"; default: return "Closed"; } } private void setState(int state) { this.state = state; setChanged(); notifyObservers(); } }
但是采用这种实现,有一个缺陷:state变量在Door类的实现中浑身扩散,就像癌症一般!
3.2 方法二: State Pattern
A. 基本的 State Pattern 实现
以上设计方式要求每个状态子类实例内部“ hold 住”一个 Door2 实例的引用,这样才能完成 Door2 实例和它的各个状态实例时间的互相通信。这种设计要求一个状态实例对应一个 Door2 实例,这样一来,一个状态实例就只能为一个 Door2 实例服务╮ ( ╯▽╰ ) ╭
客户端这样调用:
public static void main(String[] args){ Door2 door=new Door2(); //1. 初始状态 System.out.println(door.status()); //2. 转移到Opening状态 door.touch(); System.out.println(door.status()); //3. 转移到Open状态 door.complete(); System.out.println(door.status()); //4. 转移到Closing状态 door.timeout(); System.out.println(door.status()); //5. 回到Closed状态 door.complete(); System.out.println(door.status()); }
下面给出Door2类、DoorState抽象类、DoorStayOpen类的实现:
Door2:
public class Door2 extends Observable { public final DoorState CLOSED = new DoorClosed(this); public final DoorState CLOSING = new DoorClosing(this); public final DoorState OPEN = new DoorOpen(this); public final DoorState OPENING = new DoorOpening(this); public final DoorState STAYOPEN = new DoorStayOpen(this); private DoorState state = CLOSED; public void touch() { state.touch(); } public void complete() { state.complete(); } public void timeout() { state.timeout(); } public String status() { return state.status(); } protected void setState(DoorState state) { this.state = state; setChanged(); notifyObservers(); }
DoorState抽象类:
public abstract class DoorState { protected Door2 door; public abstract void touch(); public void complete() { } public void timeout() { } public String status() { String s = getClass().getName(); return s.substring(s.lastIndexOf('.') + 1); } public DoorState(Door2 door) { this.door = door; } }
DoorStayOpen类:
public class DoorStayOpen extends DoorState { public DoorStayOpen(Door2 door) { super(door); } public void touch() { door.setState(door.CLOSING); } }
B. State Pattern 实现 2 ——让状态实例( DoorState 的子类实例)为多个 Door2 实例服务
子状态 DoorOpen 实现转移时只负责返回下目标状态是什么,将状态转移的 action 留给 Door2 实例自己来做;而不是像“ A. 基本的 State Pattern 实现”那样在 DoorOpen 内部保存一个 Door2 实例的引用 door ,亲自调用 door.setState(door.STAYOPEN); 来实现状态转移
改进后的关键代码:
public class DoorOpen extends DoorState{ public DoorState touch(){ return DoorState.STAYOPEN; // 以前是 door.setState(door.STAYOPEN); } ... } public class Door2 extends Observable{ public void touch(){ state=state.touch(); // 以前是 state.touch(); // 即将转移状态的工作留给状态实例来做,事不关己高高挂起 } }
C. State Pattern 实现 3 ——让状态实例( DoorState 的子类实例)为多个 Door2 实例服务
另一种实现这种效果的方法是:将 Door2 实例作为参数传递给 DoorState 的状态转移方法,而非建立 Composite 的关联关系(将 DoorState 的子类对象作为 Door2 的属性)。
也即,用“ Dependency 依赖”(弱依赖,如调用)代替了“ Association 关联”(强依赖,如作为属性进行组合)。
4. 状态图细节
何谓 State 状态 : Generally speaking, the state of an object depends on the collective value of the object ’ s instance variables. In some cases, most of an object ’ s attributes are fairly static once set, and one attribute is dynamic and plays a prominent role in the class ’ s logic. This attribute may represent the state of the entire object and may even be named state.
4.1 State
You can subdivide a state icon into areas that show the state ’ s name and activities 活动 .
3 frequently used categories of activities are entry (what happens when the system enters the state), exit (what happens when the system leaves the state), and do (what happens while the system is in the state).
4.2 Transition s (Details: Event[Guard Condition]/Action)
You can also add some details to the transition lines. You can indicate an event that causes a transition to occur (a trigger event ) and the computation (the action ) that executes and makes the state change happen.
A guard condition : when it’s met, the transition takes place. 通常将超时作为监护条件,∵可以认为此时没有任何 event.
•源状态 Source State :即受转换影响的状态
•目标状态 Target State :当转换完成后,对象的状态
•触发事件 (Trigger) Event :用来为转换定义一个事件,包括调用、改变、信号、时间四类事件
•监护条件 (Guard Condition) :布尔表达式,决定是否激活转换、
•动作 (Action) :转换激活时的操作
几个实例:
发表评论
-
(第十章)一个xml解析器和构造器
2013-03-10 16:40 894本章的前两节“10.1 状态机”、“10.2 ... -
享元模式——Flyweight
2012-02-17 13:10 1008享元模式——Flyweig ... -
工厂方法和抽象工厂——Factory Method & Abstract Factory
2012-01-04 17:14 2044一、使用抽象工厂和工厂方法 Factory Me ... -
单例模式——Singleton
2012-01-04 17:08 967public class Singleton { ... -
观察者模式——Observer
2012-01-04 16:25 1273观察者模式—— Observer ... -
适配器模式——Adaptor(Adapter)
2012-01-01 18:23 1341适配器模式 —— Adapto ... -
装饰者模式——Decorator
2011-12-25 11:11 1132装饰者模式—— Decorator ... -
组合模式——Composite
2011-12-24 14:27 9661. Composite 定义 : ... -
构造者模式——Builder
2011-08-10 13:59 1024构造者模式——Builder 本文是《Java设计模 ... -
责任链模式——Chain of Responsibility
2011-08-10 11:26 886一、总结《Java设计模式》Chapter12 Chain o ... -
代理模式Dynamic Proxies(四、Struts2.0拦截器Interceptor)
2011-08-01 11:31 1372一、概念和注意点: Once you write a d ... -
代理模式Remote Proxies(三、RMI)
2011-08-01 09:51 1657因为我本科毕业设计中大量采用RMI实现分布式,且使用了Ecli ... -
代理模式Image Proxies(二、最朴素实现)
2011-07-31 11:55 942在前面《 代理模式Image Proxies(一、最朴素实现) ... -
命令模式——Command
2011-06-10 10:31 905偷懒一下,直接用JavaEye上chjavach老兄的文章了, ... -
代理模式Image Proxies(一、最朴素实现)
2011-06-03 09:27 1012A Classic Example: Image Prox ... -
策略模式——strategy
2011-06-02 12:36 843Strategy Pattern ...
相关推荐
管理订单状态,该上状态机吗?轻量级状态机COLA StateMachine保姆级入门教程.doc
设计模式C++学习之状态模式(State)
状态模式(State) 用意:允许一个对象当状态改变时,改变其行为。
Head First 设计模式 (十) 状态模式(State pattern) C++实现
matlab中状态机 stateflow的详细使用方法。matlab中状态机 stateflow的详细使用方法。matlab中状态机 stateflow的详细使用方法
状态模式-State Pattern状态模式-State Pattern【学习难度:,使用频率:】状态模式-State Pattern处理对象的多种状态及其相互
知道如何用java+有限状态机把大象塞进冰箱吗?
Robert C. Martin做了两个自动产生FSM代码的工具,for ...这个工具的输入是纯文本的状态机描述,自动产生符合State Pattern的代码,这样developer的工作只需要维护状态机的文本描述,没必要冒引入bug的风险去维护code。
有限状态机(Finite State Machine)是一种时序机,它源自于人们将一个复杂的问题分割成多个简单的部分来处理的思想。状态机通过时钟驱动下的有多个状态,以及状态之间的跳转规则来实现复杂的逻辑,一旦当前的状态...
在状态模式(State Pattern)中,类的行为是基于它的状态改变的。这种类型的设计模式属于行为型模式。 在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象。
设计模式——状态模式状态模式(State Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类应用场景:当控制一个对象的状态转换的条件表达式过于复杂时,把状态的判断逻辑转移到表示不同...
通用有限状态机(FSM: Finite-state machine)自动代码生成器. 可以根据配置文件,自动生成状态机代码(C++)。配置文件中只需要定义状态,跃迁条件。然后完善每个状态的动作即可。省去开发过程中手写状态机的麻烦。...
主要介绍了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
状态机c语言实现.doc 状态机原理及用法.pdf 状态机思路在单片机程序设计中的应用.doc 状态机思路在单片机程序设计中的应用.rar 状态机的两种写法.pdf 状态机的编程.rar 状态机编程 (1).doc 状态机编程.doc ...
限状态机(Finite State Machine或者Finite State Automata)是软件领域中一种重要的工具,很多东西的模型实际上就是有限状态机。
第3篇为创新没汁篇,具体讲述厂将visualSTATE牛成的代码集成到STM32的具体例广ATM取款机设计,并在最后展示了实际中——款车灯系统应用visualSTATE快速建模的过程。 本书由浅入深,循序渐进,适合刚接触基于状态机...
2009-03-10模式State:一个事件(消息)引起环境或数据的变化,可能导致状态变化,于是处理流程改变,这里的状态是把处理流程分成了很多阶段,通过状态机来根据传来的事件(消息)引发状态转换,管理状态属性和状态行为,...
式机器人状态估计——腿运动学与惯性测量单元的一致融合
第3篇为创新没汁篇,具体讲述厂将visualSTATE牛成的代码集成到STM32的具体例广ATM取款机设计,并在最后展示了实际中——款车灯系统应用visualSTATE快速建模的过程。 本书由浅入深,循序渐进,适合刚接触基于状态机...
NULL 博文链接:https://dingran.iteye.com/blog/1910161