js设计模式:状态模式

作用:

将对象的行为和状态进行分离,状态是由行为操作决定的,而不是直接控制。

同时,行为也是由状态决定的,每个状态都有自己的行为和相应的方法

行为与状态分离,可以使代码方便维护

示例:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>状态模式</title>
</head>

<body>
    <button onclick="wjtUseCom1()">王惊涛上机</button>
    <button onclick="swkUseCom1()">孙悟空上机</button>
    <button onclick="wjtAddTime()">王惊涛续费</button>
    <button onclick="aheadClose()">王惊涛提前下机</button>
    <script>
        //网吧上网

        //电脑类
        class Computer {
            constructor(code) {
                this.code = code
            }
            useName = null
            status = 'close'
            useTime = 0
            canUseTime = 0

            //快没钱了
            warning() {
                this.status = 'warning'
                console.log(`${this.code}警告:尊敬的${this.useName}先生/女士,您的余额所剩不多,请到台前及时充值,当前状态${this.status}`)
            }

            //到时关闭或者提前下机
            close(name,useFun) {
                if(name !== this.useName){
                    console.log(`你不是机器使用者,无权关闭`)
                    return 
                }
                this.status = 'close'
                this.canUseTime = 0
                this.useTime = 0
                if(useFun){
                    clearInterval(useFun)
                useFun = null
                }

                console.log('到时间了,当前状态:' + this.status)
            }

            //续费
            addTime(addTime,name) {
                if(name !== this.useName){
                    console.log(`你充错机器了吧,现在这台机器是${this.useName}在使用`)
                    return 
                }
                this.status = 'using'
                this.canUseTime += addTime
                console.log('续费成功,当前状态' + this.status)
            }

            useCom = function (name, canUseTime) {
                if (this.status !== 'close') {
                    console.log(`不好意思:${name}先生/女士${this.code}正在被${this.useName}使用,请你换一台`)
                    return
                }
                this.useName = name
                this.status = 'using'   //开机使用
                console.log(`${name}开始使用${this.code},当前状态:${this.status}`)
                this.canUseTime = canUseTime
                
                let useFun = setInterval(() => {
                    if(this.status === 'close'){
                        clearInterval(useFun)
                        useFun = null
                        this.canUseTime = 0
                        this.useTime = 0
                        return
                    }
                    this.useTime += 1000
                    console.log(`已经使用${this.useTime}了,还剩${this.canUseTime - this.useTime}`)
                    if (this.canUseTime - this.useTime === 5000) {  //时间不多了,警告提示
                        this.warning()
                    }
                    if (this.useTime === this.canUseTime) {
                        this.close(this.useName,useFun)   //时间到了,关闭

                    }
                }, 1000)
            }
        }

        let computer1 = new Computer('1号机器')
        let wjt = '王惊涛'
        let sunwukong = '孙悟空'

        const wjtUseCom1 = ()=>{
            computer1.useCom(wjt, 10000)
        }
        const swkUseCom1 = ()=>{
            computer1.useCom(sunwukong, 10000)
        }
        const wjtAddTime = ()=>{
            computer1.addTime(10000,'王惊涛')
        }
        const aheadClose = ()=>{
            computer1.close('王惊涛')
        }
        
    </script>
</body>

</html>
相关推荐
爱勇宝4 小时前
大多数人不是在使用 AI 赚钱,而是在帮 AI 公司赚钱
前端·后端·程序员
冬奇Lab5 小时前
每日一个开源项目(第143篇):page-agent - 纯 JS 的网页 GUI Agent,无需截图、无需插件、无需后端
前端·人工智能·agent
To_OC7 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
IT_陈寒9 小时前
React的这个渲染问题连官方文档都没说清楚
前端·人工智能·后端
追逐时光者11 小时前
别再满网找零散工具了,腾讯 QQ 浏览器这个“帮小忙”工具箱真能省时间
前端·后端
To_OC13 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
Asmewill13 小时前
grep&curl命令学习笔记
前端
stringwu13 小时前
Flutter 开发必备:MVI 架构的高效实现指南
前端·flutter
用户21366100357214 小时前
Vue2组件化开发与父子通信
前端·vue.js
Momo__14 小时前
TypeScript satisfies 操作符——比 as 更安全的类型守门员
前端·typescript