js设计模式-工厂模式

工厂模式

什么是工厂模式
工厂模式是一种 创建模式,用来解决创建对象的问题。根据参数类型,通过调用工厂方法来创建不同类型的对象。直营店下单给工厂,工厂造出所需的产品。具体怎么做的不管, 对于工厂来说,根据订单找另外的厂家,买来组装到一起。

适用场景:

  • 对象的构建十分复杂
  • 根据不同的条件创建不同的对象
  • 处理大量具有相同属性的小对象

例如:
手机店 -> 根据指定类型出售手机功能

function phoneShop() {}
phoneShop.prototype = {
  sellPhone: function(type) {
    var phone;
    switch(type) {
      case '苹果':
        phone = new iPhone()
        break
      case '华为': 
        phone = new Huawei()
        break
      default:
        phone = new Xiaomi()
    }
    phone.film()  // 调用贴膜功能, 给手机贴膜
    return phone
  }

}

function iPhone() {
  this.say = function() {
    console.log('iPhone: 宇宙我最贵')
  }
  this.film = function() {
    console.log('iPhone 贴膜完成')
  }
}

function Huawei() {
  this.say = function() {
    console.log('华为: 我能看到银河')
  }
  this.film = function() {
    console.log('华为贴膜完成')
  }
}

function Xiaomi() {
  this.say = function() {
    console.log('小米: 你永远买不到我')
  }
  this.film = function() {
    console.log('小米贴膜完成')
  }
}

var shop = new phoneShop()
var myPhone = shop.sellPhone('华为')
myPhone.say()

贴膜可以由手机店来做,但生产手机不应该由手机店生产。可以让厂家生产

var phoneFactory = {
  createPhone: function(type) {
    var phone;
    switch(type) {
      case '苹果':
        phone = new iPhone()
        break
      case '华为': 
        phone = new Huawei()
        break
      default:
        phone = new Xiaomi()
    }
    return phone
  }

}

function phoneShop() {}
phoneShop.prototype = {
  sellPhone: function(type) {
    var phone = phoneFactory.createPhone(type)
    phone.film()
    return phone
  }

}

function iPhone() {
  this.say = function() {
    console.log('iPhone: 宇宙我最贵')
  }
  this.film = function() {
    console.log('iPhone 贴膜完成')
  }
}

function Huawei() {
  this.say = function() {
    console.log('华为: 我能看到银河')
  }
  this.film = function() {
    console.log('华为贴膜完成')
  }
}

function Xiaomi() {
  this.say = function() {
    console.log('小米: 你永远买不到我')
  }
  this.film = function() {
    console.log('小米贴膜完成')
  }
}

var shop = new phoneShop();
var myPhone = shop.sellPhone('苹果')
myPhone.say()

一个实际点的例子

const Dom = {
    create:  function(type, url) {
        return new this.types[type](url) 
    },
    types: {
        text: function(url) {
           this.appendTo = function(parent) {
                parent.appendChild(document.createTextNode(url))
           }
        },
        image: function(url) {
           this.appendTo = function(parent) {
                let img = document.createElement('img')
                img.src = url
                parent.appendChild(img)
           }
        },
        link: function(url) {
           this.appendTo = function(parent) {
                let link = document.createElement('a')
                link.href = url
                link.appendChild(document.createTextNode(url))
                parent.appendChild(link)
           }
        }
    }
}

Dom.create('text', 'https://xxx.com').appendTo(document.body)
Dom.create('link', 'https://xxx.com').appendTo(document.body)
Dom.create('image','https://xxx.com/addons/theme//logo.acace42c.png').appendTo(document.body)

改进

const Dom = {
  create:  function(type, url) {
    return new this.types[type]
  },
  types: {
    text: function() {
      this.node = document.createTextNode('')
      this.appendTo = function(parent) {
        parent.appendChild(this.node)
        return this
      }
      this.setText = function(text) {
        this.node.data = text
        return this
      }
    },
    image: function() {
      this.node = document.createElement('img')
      this.appendTo = function(parent) {
        parent.appendChild(this.node)
        return this
      }
      this.setSrc = function(src) {
        this.node.src = src
        return this
      }
      this.setSize = function(width, height) {
        this.node.style.width = width + 'px'
        this.node.style.height = height + 'px'
        return this
      }
    },
    link: function() {
      this.node = document.createElement('a')
      this.appendTo = function(parent) {
        parent.appendChild(this.node)
        return this
      }
      this.setHref = function(href){
        this.node.href = href
        return this
      }
      this.setText = function(text) {
        this.node.appendChild(document.createTextNode(text))
        return this
      }
    }
  }
}

Dom.create('text')
    .setText('https://xxx.com')
    .appendTo(document.body)
Dom.create('link')
    .setHref('https://xxx.com')
    .setText('xxx')
    .appendTo(document.body)
Dom.create('image')
    .setSrc( 'http://xxx/11.jpg')
    .setSize(30, 30)
    .appendTo(document.body)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简单工厂 假设你想开一个自行车店,里面有几种不同品牌的自行车,可以用下面的类表示。 关于 interface 详见...
    迷路的小狮子阅读 225评论 0 0
  • 简单工厂模式 优点:能解决多个相似的问题 缺点:不能识别对象的类型 工厂模式是为了解决多个类似对象声明问题,也就是...
    bestvist阅读 413评论 0 0
  • 工厂模式介绍 什么时候用工厂模式,使用场景等? 将 new 操作单独封装 遇到 new 时,就要考虑是否该使用工厂...
    林海_Mense阅读 240评论 0 0
  • 前言 关于设计模式,是一个永远说不完的也说不清的话题。毕竟在编程的世界里,没有最好的设计模式,只有最合适的设计模式...
    VV木公子阅读 1,564评论 0 9
  • 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。通常我们所说的工厂模式是指工厂方法模...
    zfylin阅读 1,339评论 0 7