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

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

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.content
    document.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.content
    document.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.content
    document.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()

弹窗工厂一

类似于寄生模式的实现

js 复制代码
function createPop(type, msg) {
  const o = new Object()
  o.content = msg
  o.show = function () {}

  if (type === 'alert') {
  }
  if (type === 'confirm') {
  }
  if (type === 'promt') {
  }

  return o
}

弹窗工厂二

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

书本工厂

js 复制代码
function createBook(name, time, type) {
  const o = new Object()
  o.name = name
  o.time = time
  o.type = type
  o.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())
相关推荐
不听话坏1 小时前
Ignition篇(下 一) 动态执行前的事情
开发语言·前端·javascript
likeyi071 小时前
require 和 import的区别
开发语言·前端
咖啡八杯1 小时前
GoF设计模式——迭代器模式
java·后端·设计模式·迭代器模式
pany2 小时前
做 AI 友好的开源 Vue3 模板 🌈
前端·vue.js·ai编程
小二·2 小时前
React 19 + Next.js 15 现代前端开发实战:App Router / Server Components / 流式渲染
前端·javascript·react.js
谙忆10243 小时前
前端图片直传对象存储:OSS/S3 预签名 URL、STS 临时凭证与回调校验
前端
ttod_qzstudio3 小时前
【软考设计模式】外观模式:复杂子系统的“门面“封装与代码填空精讲
设计模式·外观模式
CHNE_TAO_EMSM4 小时前
Android studio 打开文件时自动下载源码
前端·javascript·android studio
一孤程5 小时前
Airtest自动化测试第五篇:小程序与Web测试——跨平台自动化全覆盖
前端·自动化测试·小程序·自动化·测试·airtest
IT_陈寒5 小时前
SpringBoot自动配置不是你以为的那样的智能
前端·人工智能·后端