State Design Pattern
注:
转载自 https://www.geeksforgeeks.org/state-design-pattern/ 【以便查阅,非原创】
State Design Pattern
State pattern is one of the behavioral design pattern. State design pattern is used when an Object changes its behavior based on its internal state.
If we have to change behavior of an object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state. State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations.
UML Diagram of State Design Pattern

- Context: Defines an interface to client to interact. It maintains references to concrete state object which may be used to define current state of object.
- State: Defines interface for declaring what each concrete state should do.
- ConcreteState: Provides implementation for methods defined in State.
Example of State Design Pattern
In below example, we have implemented a mobile state scenario . With respect to alerts, a mobile can be in different states. For example, vibration and silent. Based on this alert state, behavior of the mobile changes when an alert is to be done.// Java program to demonstrate working of// State Design PatterninterfaceMobileAlertState{publicvoidalert(AlertStateContext ctx);}classAlertStateContext{privateMobileAlertState currentState;publicAlertStateContext(){currentState =newVibration();}publicvoidsetState(MobileAlertState state){currentState = state;}publicvoidalert(){currentState.alert(this);}}classVibrationimplementsMobileAlertState{@Overridepublicvoidalert(AlertStateContext ctx){System.out.println("vibration...");}}classSilentimplementsMobileAlertState{@Overridepublicvoidalert(AlertStateContext ctx){System.out.println("silent...");}}classStatePattern{publicstaticvoidmain(String[] args){AlertStateContext stateContext =newAlertStateContext();stateContext.alert();stateContext.alert();stateContext.setState(newSilent());stateContext.alert();stateContext.alert();stateContext.alert();}}Output:
vibration...
vibration...
silent...
silent...
silent...Advantages of State Design Pattern
- With State pattern, the benefits of implementing polymorphic behavior are evident, and it is also easier to add states to support additional behavior.
- In the State design pattern, an object’s behavior is the result of the function of its state, and the behavior gets changed at runtime depending on the state. This removes the dependency on the if/else or switch/case conditional logic. For example, in the TV remote scenario, we could have also implemented the behavior by simply writing one class and method that will ask for a parameter and perform an action (switch the TV on/off) with an if/else block.
- The State design pattern also improves Cohesion since state-specific behaviors are aggregated into the ConcreteState classes, which are placed in one location in the code.
Disadvantages of State Design Pattern
- The State design pattern can be used when we need to change state of object at runtime by inputting in it different subclasses of some State base class. This circumstance is advantage and disadvantage in the same time, because we have a clear separate State classes with some logic and from the other hand the number of classes grows up.
State Design Pattern的更多相关文章
- State Design Pattern 状态设计模式
设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换. 有点像Finite Automata算法,两者的思想是一样的. 会Finite Automata,那么这个设计模式就非常easy了. # ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
- [转]Design Pattern Interview Questions - Part 3
State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- Design Pattern: Observer Pattern
1. Brief 一直对Observer Pattern和Pub/Sub Pattern有所混淆,下面打算通过这两篇Blog来梳理这两种模式.若有纰漏请大家指正. 2. Use Case 首先我们来面 ...
- Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern
Monad typeclass不是一种类型,而是一种程序设计模式(design pattern),是泛函编程中最重要的编程概念,因而很多行内人把FP又称为Monadic Programming.这其中 ...
随机推荐
- WD MyBook Live Duo 重装教程
9102年了,我还在用MBL DUO 前情提要:这个设备基础配置是3T*2,但是近期两块3T硬盘需要另做他用,因此只能用2块1T的硬盘来替换了,所以就免不了要重灌WD的固件.可能是由于设备太老吧,那个 ...
- Qt编写安防视频监控系统16-设备播放
一.前言 设备播放模块是后面增加的,核心就是通过组合rtsp视频流地址来播放实时视频和历史视频,目前市面上很多厂家比如排第一的海康都是支持直接rtsp通过NVR来播放某个通道视频流和回放某个通道的视频 ...
- PMP 第8章错题总结
1.项目经理为项目的可交付成果定义验收标准.这些应记录在项目范围说明书2.项目的总体预算是“成本基准+管理储备”,成本基准里本身已包含了应急储备.工作包成本估算.活动成本估算3.范围基准包含----项 ...
- Ubuntu16.04安装Consul
1.下载安装包 https://www.consul.io/downloads.html wget https://releases.hashicorp.com/consul/1.5.3/consul ...
- 将pip源设置国内源
windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %appdata% (3)在这里面新建一个文件夹 pip (4)在pip文件夹里面新建一个文件叫做 pip.ini , ...
- 【转】kettle7.1资源库无法打开,找不到connect按钮的问题处理
转自:https://www.aboutyun.com/home.php?mod=space&uid=71645&do=blog&id=3535 kettle是一个比较好用的E ...
- WeQuant教程—1.5 实盘运行须知
为了保证实盘交易程序能够正常稳定地运行,同时保护您在使用时账户资金的安全,我们设计了一些规则和机制.了解这些机制有助于您更快上手实盘交易. 启动前检查机制 在实盘交易程序启动前,系统会执行一次检查,出 ...
- Xshell连接SqlPlus无法使用退格、删除键
问题:在使用xshell连接CentOS7,进入SQLPLUS进行命令操作时,如果输错了信息,无法进行退格键删除(显示“^H”),同样按删除键,显示“^[[3~”. 解决:网上查找了相关资料,可以通过 ...
- 转 java 8 lamba stream
一直在写中间件相关的代码,提供SDK给业务方使用,但很多业务方还一直停留在1.7版本,迟迟不升级,为了兼容性,不敢在代码中使用Java8的一些新特性,比如Stream之类的,虽然不能用,但还是要学一下 ...
- shell 编写脚本批量Ping IP
服务器总是一下子买了很多的段的ip.通过绑定后,也不知道这些ip是否绑定成功,所以就写了一个shell脚本,把ip输好,批量ping一下,看是不是都能ping通. 脚本如下: 此外.还有一个ip文件, ...