(四)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()

弹窗工厂一

类似于寄生模式的实现

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
}

弹窗工厂二

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 = 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. ()js前端开发设计模式简单工厂模式

    2024-07-22 13:58:01       18 阅读
  2. ()js前端开发设计模式工厂方法模式

    2024-07-22 13:58:01       20 阅读
  3. (三)js前端开发设计模式工厂模式

    2024-07-22 13:58:01       16 阅读
  4. (二)js前端开发设计模式单例模式

    2024-07-22 13:58:01       18 阅读
  5. (一)js前端开发设计模式前篇对象

    2024-07-22 13:58:01       15 阅读
  6. Spring设计模式简单工厂模式

    2024-07-22 13:58:01       53 阅读
  7. 大话设计模式简单工厂模式

    2024-07-22 13:58:01       35 阅读
  8. 前端设计模式工厂模式

    2024-07-22 13:58:01       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 13:58:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 13:58:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 13:58:01       45 阅读
  4. Python语言-面向对象

    2024-07-22 13:58:01       55 阅读

热门阅读

  1. HOW - CSS 定义颜色值

    2024-07-22 13:58:01       16 阅读
  2. Python @staticmethod、super().__init__()和self

    2024-07-22 13:58:01       17 阅读
  3. WHAT - 通过 shadcn 组件源码学习 React

    2024-07-22 13:58:01       13 阅读
  4. 探索 PHP 与 JD 详情 API 接口的连接奥秘

    2024-07-22 13:58:01       20 阅读
  5. 多个返回值QT/C++

    2024-07-22 13:58:01       17 阅读
  6. C# --- .Net Framework中的Binding Redirect

    2024-07-22 13:58:01       19 阅读