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 Pattern
interface
MobileAlertState
{
public
void
alert(AlertStateContext ctx);
}
class
AlertStateContext
{
private
MobileAlertState currentState;
public
AlertStateContext()
{
currentState =
new
Vibration();
}
public
void
setState(MobileAlertState state)
{
currentState = state;
}
public
void
alert()
{
currentState.alert(
this
);
}
}
class
Vibration
implements
MobileAlertState
{
@Override
public
void
alert(AlertStateContext ctx)
{
System.out.println(
"vibration..."
);
}
}
class
Silent
implements
MobileAlertState
{
@Override
public
void
alert(AlertStateContext ctx)
{
System.out.println(
"silent..."
);
}
}
class
StatePattern
{
public
static
void
main(String[] args)
{
AlertStateContext stateContext =
new
AlertStateContext();
stateContext.alert();
stateContext.alert();
stateContext.setState(
new
Silent());
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.这其中 ...
随机推荐
- -[UITableView copyWithZone:]: unrecognized selector sent to instance 0x7XXXXXX00
-[UITableView copyWithZone:]: unrecognized selector sent to instance 0x7XXXXXX00 -[Class copyWithZon ...
- Web容器中DefaultServlet详解 JspServlet DefaultServlet
Web容器中DefaultServlet详解 https://blog.csdn.net/qq_30920821/article/details/78328608 Web容器中DefaultServl ...
- MongoDB开发深入之二:索引
索引分类: 默认索引 单一索引 复合索引 多键索引(数组索引) 全文检索索引 2dsphere 索引 2D索引 ...... 索引属性: 到期TTL 唯一索引 部分索引 稀疏索引 索引通常能够极大的提 ...
- 【NPDP笔记】第六章 市场研究
6.1 产品市场研究 客户心声Voice of Customer,为了找出问题的解决方法,引导消费者经历一系列的情景并进行结构化的深度访谈,以提炼客户需求的过程 在产品开发决策中,需要获取的信息 ...
- Jeecg 支持多视图设置
<!-- 视图解析器1:html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 --> <bean id="freemar ...
- Appium 中使用 pressKeyCode 方法不起作用也没有报错
为了使 appium 支持 Android 系统 7 及以上,automationName 使用了 UIAutomator2.但是发现,使用androidDriver.pressKeyCode(And ...
- Excel在数据表中悬停鼠标显示数据值
打开一个实例文档,鼠标移动到数据表中,在柱状图上中悬停鼠标,可以看到没有显示数据的值. 鼠标单击左上角的<文件>按钮. 然后单击<选项>按钮. 在弹出的excel选项对话框中单 ...
- 前端与算法 leetcode 27.移除元素
目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...
- linux vi/vim 编辑器学习总结
linux vi/vim 编辑器学习总结 很多时候我们开发人员不可避免的会涉及到项目部署,这个时候使用的就不是我们所熟知的 Windows 和 Mac OS 操作系统了,而是类 Unix 系统.项目部 ...
- static binding/dynamic binding
static binding/dynamic binding class Person { private void who() { System.out.println("Inside p ...