The key to the state model is to distinguish the internal state of things. Changes in the internal state of things often bring about changes in the behavior of things.
When the light is on, if you press the switch, the light will switch to off state; if you press the switch again, the light will turn on again. The same switch behaves differently in different states.
1. Finite state machine
- The total number of states (state) is limited.
- At any moment, you are only in one state.
-
Under certain conditions, there will be a transition from one state to another.
Allows an object to change its behavior when its internal state changes, making the object appear to modify its class.
Explanation:
(1) Encapsulate the state into an independent class and delegate the request to the current state object. When the internal state of the object changes, different behavior changes will occur.
(2) The objects used have completely different behaviors (delegation effect) in different states
When it comes to encapsulation, priority is generally given to encapsulating the behavior of the object rather than the state of the object.
But in the state pattern, it's just the opposite. The key to the state pattern is to encapsulate each state of things into a separate class.
2. Example
Lighting program (low light –> strong light –> light off) cycle
// 关灯 var OffLightState = function(light) { this.light = light; }; // 弱光 var OffLightState = function(light) { this.light = light; }; // 强光 var StrongLightState = function(light) { this.light = light; }; var Light = function(){ /* 开关状态 */ this.offLight = new OffLightState(this); this.weakLight = new WeakLightState(this); this.strongLight = new StrongLightState(this); /* 快关按钮 */ this.button = null; }; Light.prototype.init = function() { var button = document.createElement("button"), self = this; this.button = document.body.appendChild(button); this.button.innerHTML = '开关'; this.currentState = this.offLight; this.button.click = function() { self.currentState.buttonWasPressed(); } }; // 让抽象父类的抽象方法直接抛出一个异常(避免状态子类未实现buttonWasPressed方法) Light.prototype.buttonWasPressed = function() { throw new Error("父类的buttonWasPressed方法必须被重写"); }; Light.prototype.setState = function(newState) { this.currentState = newState; }; /* 关灯 */ OffLightState.prototype = new Light(); // 继承抽象类 OffLightState.prototype.buttonWasPressed = function() { console.log("关灯!"); this.light.setState(this.light.weakLight); } /* 弱光 */ WeakLightState.prototype = new Light(); WeakLightState.prototype.buttonWasPressed = function() { console.log("弱光!"); this.light.setState(this.light.strongLight); }; /* 强光 */ StrongLightState.prototype = new Light(); StrongLightState.prototype.buttonWasPressed = function() { console.log("强光!"); this.light.setState(this.light.offLight); };
PS: Additional explanation
The OffLightState, WeakLightState, StrongLightState constructors must be advanced.
new A("a"); var A = function(a) { console.log(a) } new B("b"); function B(b) { console.log(b); }
Function declarations will be hoisted before ordinary variables.
3. Performance optimization points
(1) How to manage the creation and destruction of state objects?
The first is only created when the state object is needed and then destroyed (the state object is relatively large, preferred),
The other is to create all state objects at the beginning and never destroy them (state changes frequently).
(2) Use flyweight mode to share a state object.
4. JavaScript version of state machine
(1) Directly delegate the request to a literal object for execution through the Function.prototype.call method
// 状态机 var FSM = { off: { buttonWasPressed: function() { console.log("关灯"); this.button.innerHTML = "下一次按我是开灯"; // 这是Light上的属性!!! this.currState = FSM.on; // 这是Light上的属性!!! } }, on: { buttonWasPressed: function() { console.log("开灯"); this.button.innerHTML = "下一次按我是关灯"; this.currState = FSM.off; } }, }; var Light = function() { this.currState = FSM.off; // 设置当前状态 this.button = null; }; Light.prototype.init = function() { var button = document.createElement("button"); self = this; button.innerHTML = "已关灯"; this.button = document.body.appendChild(button); this.button.onclick = function() { // 请求委托给FSM状态机 self.currState.buttonWasPressed.call(self); } } var light = new Light(); light.init();
(2)Use delegate function
var delegate = function(client, delegation) { return { buttonWasPressed: function() { return delegation.buttonWasPressed.apply(client, arguments); } }; }; // 状态机 var FSM = { off: { buttonWasPressed: function() { console.log("关灯"); this.button.innerHTML = "下一次按我是开灯"; this.currState = this.onState; } }, on: { buttonWasPressed: function() { console.log("开灯"); this.button.innerHTML = "下一次按我是关灯"; this.currState = this.offState; } }, }; var Light = function() { this.offState = delegate(this, FSM.off); this.onState = delegate(this, FSM.on); this.currState = this.offState; // 设置当前状态 this.button = null; }; Light.prototype.init = function() { var button = document.createElement("button"); self = this; button.innerHTML = "已关灯"; this.button = document.body.appendChild(button); this.button.onclick = function() { // 请求委托给FSM状态机 self.currState.buttonWasPressed(); } } var light = new Light(); light.init();
I hope this article will be helpful to everyone learning JavaScript programming.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
