zoukankan      html  css  js  c++  java
  • JavaScript设计模式 -- 工厂模式

    案例一:

    function Cat(name, color) {
        return {
            name: name || 'cat',
            color: color || 'black'
        }
    }
    
    const cat1 = new Cat('kity', 'white')
    const cat2 = new Cat()
    

      

    案例二:

    const BicycleShop = function () {}
    
    BicycleShop.prototype = {
        sellBicycle: function (model) {
            const bicycle = this.createBicycle(model)
            return bicycle
        },
        createBicycle: function(model) {
            throw new Error('before use it, must be use keyword new')
        }
    }
    
    const OracleBicycle = function () {}
    inherits(OracleBicycle, BicycleShop)
    OracleBicycle.prototype.createBicycle = function(model) {
        let bicycle
        switch(model) {
            case 'speedster':
                bicycle = new OracleSpeedster()
                break
            case 'lowrider':
                bicycle = new OracleLowrider()
                break
            case 'Alien':
                bicycle = new OracleAlien()
                break
            default:
                bicy = new OracleComfortCruiser()
        }
        return bicycle
    }
    function OracleSpeedster() {
        console.log('OracleSpeedster')
    }
    function OracleLowrider() {
        console.log('OracleLowrider')
    }
    function OracleAlien() {
        console.log('OracleAlien')
    }
    function OracleComfortCruiser() {
        console.log('OracleComfortCruiser')
    }
    
    const IBMBicycleShop = function () {}
    inherits(IBMBicycleShop, BicycleShop)
    IBMBicycleShop.prototype.createBicycle = function(model) {
        let bicycle
        switch(model) {
            case 'speedster':
                bicycle = new IBMSpeedster()
                break
            case 'lowrider':
                bicycle = new IBMLowrider()
                break
            case 'Alien':
                bicycle = new IBMAlien()
                break
            default:
                bicycle = new IBMComfortCruiser()
        }
        return bicycle
    }
    function IBMSpeedster() {
        console.log('IBMSpeedster')
    }
    function IBMLowrider() {
        console.log('IBMLowrider')
    }
    function IBMAlien() {
        console.log('IBMAlien')
    }
    function IBMComfortCruiser() {
        console.log('IBMComfortCruiser')
    }
    
    function inherits(child, base) {
        child.prototype = Object.create(base.prototype)
    }
    
    const oracleShop = new OracleBicycle()
    oracleShop.sellBicycle('speedster')
    
    const ibmShop = new IBMBicycleShop()
    ibmShop.sellBicycle('IBMAlien')
    

      

  • 相关阅读:
    集合类--容器
    《学习之道》第十一章理解
    文件操作引出流(一)Stream和File.Create(path)
    整理文件操作(五)
    整理文件操作(四)Image.FromFile(path)
    整理文件操作(三)File.Exists(path)和new FileInfo(path).Exists
    整理文件操作(二)File和FileInfo
    整理文件操作(一)逻辑流程
    《学习之道》第十一章先理解再去记忆
    《学习之道》第十一章再次强调激发感官
  • 原文地址:https://www.cnblogs.com/diantao/p/13667458.html
Copyright © 2011-2022 走看看