您的位置:首页 > 游戏 > 手游 > (四)js前端开发中设计模式之简单工厂模式

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

2024/10/6 2:25:37 来源:https://blog.csdn.net/qq_27702739/article/details/140583739  浏览:    关键词:(四)js前端开发中设计模式之简单工厂模式

简单工厂模式,又叫静态工厂方法,由一个工厂对象决定创建出哪一种产品类的实例,主要用来创建同一类对象

let LoginAlert = function (msg) {this.content = msg
}
LoginAlert.prototype = {show() {const div = document.createElement('div')div.style.cssText = `min-width: 100px; height: 50px; background-color: red;color: white;text-align:center;line-height: 50px;margin-bottom: 10px;`div.innerHTML = this.contentdocument.documentElement.appendChild(div)setTimeout(() => {div.remove()}, 5000)}
}let userNameAlert = new LoginAlert('用户名不能为空')
userNameAlert.show()let passwordAlert = new LoginAlert('密码不能为空')
passwordAlert.show()let LoginConfirm = function (msg) {this.content = msg
}LoginConfirm.prototype = {show() {const div = document.createElement('div')div.id = 'login-confirm'div.style.cssText = `min-width: 100px; height: 50px; background-color: red;color: white;text-align:center;line-height: 50px;margin-bottom: 10px;`div.innerHTML = this.contentdocument.documentElement.appendChild(div)this.createRegBtn()// setTimeout(() => {//   div.remove()// }, 3000)},createRegBtn() {const btn = document.createElement('button')btn.innerHTML = '注册'const parent = document.getElementById('login-confirm')parent.appendChild(btn)}
}const loginFailConfirm = new LoginConfirm('登录失败')
loginFailConfirm.show()let LoginPromt = function (msg) {this.content = msg
}LoginPromt.prototype = {show() {const div = document.createElement('div')div.id = 'login-promt'div.style.cssText = `min-width: 100px; height: 50px; background-color: red;color: white;text-align:center;line-height: 50px;margin-bottom: 10px;`div.innerHTML = this.contentdocument.documentElement.appendChild(div)this.createRegBtn()// setTimeout(() => {//   div.remove()// }, 3000)}},createRegBtn() {const btn = document.createElement('button')btn.innerHTML = '确认'const btn1 = document.createElement('button')btn1.innerHTML = '取消'const parent = document.getElementById('login-promt')parent.appendChild(btn)parent.appendChild(btn1)}
}const loginPromt = new LoginPromt('登录成功')
loginPromt.show()

弹窗工厂一

类似于寄生模式的实现

function createPop(type, msg) {const o = new Object()o.content = msgo.show = function () {}if (type === 'alert') {}if (type === 'confirm') {}if (type === 'promt') {}return o
}

弹窗工厂二

const PopFactory = function (name) {switch (name) {case 'alert':return new LoginAlert('用户名不能为空')case 'confirm':return new LoginConfirm('登录失败')case 'promt':return new LoginPromt('登录成功')}
}

书本工厂

function createBook(name, time, type) {const o = new Object()o.name = nameo.time = timeo.type = typeo.getName = function () {return this.name}return o
}const book1 = createBook('js book', 20, 'js')
console.log(book1.getName())
const book2 = createBook('css book', 10, 'css')
console.log(book2.getName())

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com