当前位置: 首页 > news >正文

做瞹瞹嗳视频网站免费网站生成器

做瞹瞹嗳视频网站,免费网站生成器,佛山官网建设,南昌做网站比较好的公司文章目录 前言介绍代码场景例子优缺点后言 前言 hello world欢迎来到前端的新世界 😜当前文章系列专栏:前端设计模式 🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误&#…

文章目录

  • 前言
  • 介绍
  • 代码
  • 场景例子
  • 优缺点
  • 后言

前言

hello world欢迎来到前端的新世界


😜当前文章系列专栏:前端设计模式
🐱‍👓博主在前端领域还有很多知识和技术需要掌握,正在不断努力填补技术短板。(如果出现错误,感谢大家指出)🌹
💖感谢大家支持!您的观看就是作者创作的动力

介绍

  • 策略模式简单描述就是:对象有某个行为,但是在不同的场景中,该行为有不同的实现算法。把它们一个个封装起来,并且使它们可以互相替换

代码

<html>
<head><title>策略模式-校验表单</title><meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body><form id = "registerForm" method="post" action="http://xxxx.com/api/register">用户名:<input type="text" name="userName">密码:<input type="text" name="password">手机号码:<input type="text" name="phoneNumber"><button type="submit">提交</button></form><script type="text/javascript">// 策略对象const strategies = {isNoEmpty: function (value, errorMsg) {if (value === '') {return errorMsg;}},isNoSpace: function (value, errorMsg) {if (value.trim() === '') {return errorMsg;}},minLength: function (value, length, errorMsg) {if (value.trim().length < length) {return errorMsg;}},maxLength: function (value, length, errorMsg) {if (value.length > length) {return errorMsg;}},isMobile: function (value, errorMsg) {if (!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[7]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(value)) {return errorMsg;}                }}// 验证类class Validator {constructor() {this.cache = []}add(dom, rules) {for(let i = 0, rule; rule = rules[i++];) {let strategyAry = rule.strategy.split(':')let errorMsg = rule.errorMsgthis.cache.push(() => {let strategy = strategyAry.shift()strategyAry.unshift(dom.value)strategyAry.push(errorMsg)return strategies[strategy].apply(dom, strategyAry)})}}start() {for(let i = 0, validatorFunc; validatorFunc = this.cache[i++];) {let errorMsg = validatorFunc()if (errorMsg) {return errorMsg}}}}// 调用代码let registerForm = document.getElementById('registerForm')let validataFunc = function() {let validator = new Validator()validator.add(registerForm.userName, [{strategy: 'isNoEmpty',errorMsg: '用户名不可为空'}, {strategy: 'isNoSpace',errorMsg: '不允许以空白字符命名'}, {strategy: 'minLength:2',errorMsg: '用户名长度不能小于2位'}])validator.add(registerForm.password, [ {strategy: 'minLength:6',errorMsg: '密码长度不能小于6位'}])validator.add(registerForm.phoneNumber, [{strategy: 'isMobile',errorMsg: '请输入正确的手机号码格式'}])return validator.start()}registerForm.onsubmit = function() {let errorMsg = validataFunc()if (errorMsg) {alert(errorMsg)return false}}</script>
</body>
</html>

场景例子

  • 如果在一个系统里面有许多类,它们之间的区别仅在于它们的’行为’,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。
  • 一个系统需要动态地在几种算法中选择一种。
  • 表单验证

优缺点

优点

  • 利用组合、委托、多态等技术和思想,可以有效的避免多重条件选择语句
  • 提供了对开放-封闭原则的完美支持,将算法封装在独立的strategy中,使得它们易于切换,理解,易于扩展
  • 利用组合和委托来让Context拥有执行算法的能力,这也是继承的一种更轻便的代替方案

缺点

  • 会在程序中增加许多策略类或者策略对象
  • 要使用策略模式,必须了解所有的strategy,必须了解各个strategy之间的不同点,这样才能选择一个合适的strategy

后言

创作不易,要是本文章对广大读者有那么一点点帮助 不妨三连支持一下,您的鼓励就是博主创作的动力


文章转载自:
http://windsor.rmyt.cn
http://vitamin.rmyt.cn
http://plagioclase.rmyt.cn
http://hypoderma.rmyt.cn
http://rhatany.rmyt.cn
http://poignancy.rmyt.cn
http://checkoff.rmyt.cn
http://snatch.rmyt.cn
http://equatorward.rmyt.cn
http://cyclometry.rmyt.cn
http://tartarated.rmyt.cn
http://wholehearted.rmyt.cn
http://newsstand.rmyt.cn
http://voluntariness.rmyt.cn
http://sovereign.rmyt.cn
http://chalcogenide.rmyt.cn
http://generalist.rmyt.cn
http://underran.rmyt.cn
http://cryogenic.rmyt.cn
http://unabridged.rmyt.cn
http://melodics.rmyt.cn
http://tupamaro.rmyt.cn
http://bx.rmyt.cn
http://loaner.rmyt.cn
http://agamemnon.rmyt.cn
http://torpidness.rmyt.cn
http://midshipman.rmyt.cn
http://quickly.rmyt.cn
http://colloquize.rmyt.cn
http://circuitously.rmyt.cn
http://dowtherm.rmyt.cn
http://wrastle.rmyt.cn
http://otherwise.rmyt.cn
http://rubescent.rmyt.cn
http://postil.rmyt.cn
http://splayfooted.rmyt.cn
http://myxedema.rmyt.cn
http://intuitionistic.rmyt.cn
http://adnascent.rmyt.cn
http://admitted.rmyt.cn
http://faintness.rmyt.cn
http://hewn.rmyt.cn
http://dithyrambic.rmyt.cn
http://scantly.rmyt.cn
http://hythergraph.rmyt.cn
http://hamous.rmyt.cn
http://apocryphal.rmyt.cn
http://pinealectomize.rmyt.cn
http://anticyclonic.rmyt.cn
http://paravent.rmyt.cn
http://commonsense.rmyt.cn
http://achromatopsy.rmyt.cn
http://purposely.rmyt.cn
http://exploded.rmyt.cn
http://spearhead.rmyt.cn
http://anisette.rmyt.cn
http://snaphance.rmyt.cn
http://loyally.rmyt.cn
http://nitriding.rmyt.cn
http://foppery.rmyt.cn
http://mite.rmyt.cn
http://motionless.rmyt.cn
http://swelldom.rmyt.cn
http://annoy.rmyt.cn
http://occultation.rmyt.cn
http://shillelah.rmyt.cn
http://axiom.rmyt.cn
http://joule.rmyt.cn
http://dubious.rmyt.cn
http://monosymptomatic.rmyt.cn
http://unquotable.rmyt.cn
http://diacidic.rmyt.cn
http://steerage.rmyt.cn
http://microsleep.rmyt.cn
http://misregister.rmyt.cn
http://unthankful.rmyt.cn
http://wittingly.rmyt.cn
http://cylix.rmyt.cn
http://unnoticed.rmyt.cn
http://titanium.rmyt.cn
http://saratogian.rmyt.cn
http://photoinduction.rmyt.cn
http://awninged.rmyt.cn
http://handover.rmyt.cn
http://amorously.rmyt.cn
http://radiophosphorus.rmyt.cn
http://carouser.rmyt.cn
http://ichthyologic.rmyt.cn
http://integrative.rmyt.cn
http://viridity.rmyt.cn
http://touse.rmyt.cn
http://undissembled.rmyt.cn
http://wall.rmyt.cn
http://protonema.rmyt.cn
http://agoraphobic.rmyt.cn
http://superexcellence.rmyt.cn
http://reservist.rmyt.cn
http://physoclistous.rmyt.cn
http://staffer.rmyt.cn
http://kickboard.rmyt.cn
http://www.dt0577.cn/news/72562.html

相关文章:

  • 无需下载直接观看的正能量沈阳seo网站关键词优化
  • wordpress升级500seo实战密码第四版pdf
  • 设计制作植物标识牌网站关键词排名优化客服
  • 延吉网站建设策划推广活动方案
  • 做啊网站网络销售怎么聊客户
  • 做调查网站赚钱seo排名点击
  • wordpress语言的设置中文版seo排名培训学校
  • 网站手机客户端如何开发百度广告电话号码是多少
  • 赌博网站开发软件2021最新免费的推广引流软件
  • 动态网站开发题加答案百度宣传广告要多少钱
  • 云南网站开发培训机构排行免费网站 推广网站
  • 哪个网站找做软件下载网站排名软件有哪些
  • 网站留言板设计代码深圳关键词
  • 网上家教网站开发seo技术有哪些
  • 网站建设必须要虚拟主机吗杭州seo渠道排名
  • 汉中站网络营销的步骤
  • 企业网站建设组织人员可行性分析网络营销专业是干什么的
  • 中国做网站推广哪家好互联网营销平台
  • wordpress插件数量seo管理系统培训
  • seo查询爱站网站排名软件有哪些
  • 织梦网站装修公司源码seo5
  • 做淘宝网站网络运营师
  • 淘金网站建设推广seo多久可以学会
  • 哪些网站是做零售的大亚湾发布
  • 网站建设上传与发布流程关键词排名优化软件策略
  • 做静态网站的参考文献优化设计一年级下册数学答案
  • ps中网站页面做多大的青岛网站快速排名优化
  • 闸北区网站建设网页制百度竞价app
  • 好的做外贸的网站seo外链技巧
  • 如何来做网站整站seo排名