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

(四)js前端开发中设计模式之工厂方法模式

工厂方法模式,通过对产品类的抽象,使其创建业务主要用于负责创建多类产品的实例

const Java = function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'green'document.getElementById('container').appendChild(oDiv)})()
}const Php = function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'red'document.getElementById('container').appendChild(oDiv)})()
}const JavaScript = function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'pink'document.getElementById('container').appendChild(oDiv)})()
}

简单工厂模式,扩展的话需要改动两个地方,一个是工厂类,一个是具体产品类

function JobFactory(type, content) {switch (type) {case 'java':return new Java(content)breakcase 'javascript':return new JavaScript(content)break}
}JobFactory('java', 'Java 培训哪家强1111')

安全模式实例

var Demo = function () {}
Demo.prototype.show = function () {console.log('show')
}//正常使用
let d1 = new Demo()
d1.show() //show//非正常使用
// let d2 = Demo()
// d2.show() //报错//改造上面的 democonst Demo2 = function () {// if (this instanceof Demo2) {// return this// } else {// return new Demo2()// }console.log('🚀 ~ Demo2 ~ this:', this)if (!(this instanceof Demo2)) {console.log('🚀 ~ Demo2 ~ new Demo2():', new Demo2())return new Demo2()}
}Demo2.prototype.show = function () {console.log('show 999')
}let d2 = Demo2()
d2.show() //报错

工厂方法模式,扩展的话只需要改动工厂类

const Factory = function (type, content) {if (this instanceof Factory) {return new this[type](content)} else {return new Factory(type, content)}
}Factory.prototype = {//注意方法这里不能简写需要:functionJava: function (content) {//注意这里不能加thisthis.content = contentconsole.log('🚀 ~ content11111:', content);(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'green'document.getElementById('container').appendChild(oDiv)})()},JavaScript: function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'red'document.getElementById('container').appendChild(oDiv)})()},UIEvent: function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'pink'document.getElementById('container').appendChild(oDiv)})()},Php: function (content) {this.content = content;(function () {let oDiv = document.createElement('div')oDiv.innerHTML = contentoDiv.style.color = 'blue'document.getElementById('container').appendChild(oDiv)})()}
}

实现

const data = [{type: 'Java',content: 'Java 培训哪家强'},{type: 'JavaScript',content: 'JavaScript 培训哪家强'},{type: 'Php',content: 'PHP 培训哪家强'},{type: 'UIEvent',content: 'UIEvent 培训哪家强'}
]//创建实例
for (let i = 0; i < data.length; i++) {Factory(data[i].type, data[i].content)
}
http://www.lryc.cn/news/406661.html

相关文章:

  • 新版GPT-4omini上线!快!真TM快!
  • 【Unity】RPG2D龙城纷争(十七)敌方常规AI(Normal)的实现
  • Tracy 小笔记:微信小程序 mpx 雷达图的实现
  • Unity UGUI 之 Input Field
  • SpringBoot接入mongodb例子,并有增删改查功能
  • 类和对象(三)
  • Android SurfaceFlinger——GraphicBuffer初始化(二十九)
  • pytest:4种方法实现 - 重复执行用例 - 展示迭代次数
  • 一文入门SpringSecurity 5
  • IPython的HTML魔法:%%html_header命令全解析
  • 将SQL中的占位符替换成参数
  • 锁相环 vivado FPGA
  • 英语科技写作 希拉里·格拉斯曼-蒂(英文版)pdf下载
  • 《Dynamic Statistical Learning in Massive Datastreams》论文阅读笔记
  • 【数据分享】2008-2022年我国省市县三级的逐日NO2数据(excel\shp格式)
  • JavaEE (1)
  • 事务、函数和索引
  • Android APP 基于RecyclerView框架工程(知识体系积累)
  • 【iOS】GCD
  • C语言 | Leetcode C语言题解之第282题给表达式添加运算符
  • 如何使用 API list 极狐GitLab 容器镜像仓库中的 tag?
  • 粒子群算法PSO优化BP神经网络(PSO-BP)回归预测——Python和MATLAB实现
  • React-router路由配置及跳转
  • vue3【实战】可编辑的脱敏信息
  • S71200 - 笔记
  • linux系统查历史cpu使用数据(使用sar 查询cpu和网络占用最近1个月历史数据)。
  • Edge浏览器加载ActiveX控件
  • BUG与测试用例设计
  • 怎么在使用select2时,覆盖layui的下拉框样式
  • MacOSM1 配置Miniconda环境,并设置自启动