JS设计模式

单例模式

var single =(function(){
    var unique;
    function getInstance(){
       if(unique===undefined){
            unique=new Construct();
        } 
        return unique;
    }
    function Construct(){
       
    }
    return{
         return getInstance:getInstance
    }
})();        

构造函数模式:

function Car (color,price,brand){
  if(!(this instanceof Car)){
    return new Car(color,year,brand);
  }
  this.color=color;
  this.price=price;
  this.brand=brand;
  this.output=function(){
    return this.color+"-"+this.price+this.brand;
  }
}
var tom = new Car('1','2','3');
tom.output();

建造者模式:

function workerBuilder(){
  this.workOne=function(){
    //建房子的骨架
  }
  this.workTwo=function(){
    //建厨房
  }
  this.workThree=function(){
    //建客厅
  }
  this.workFour=function(){
    //建卧室
  }
  this.getResult=function(){
    var house = new house();
    return house;
  }
}


function Director(){
  this.coustruct=function(builder){
    builder.workOne();
    builder.workTwo();
    builder.workThree();
    builder.workFour();
  }
}

function House(){
  this.houseFrame="";
  this.houseKitchen="";  
  this.houseRoom="";
  this.linvingRoom="";
}

var builder = new workerBuilder;
var director=new Director();
director.coustruct(builder);
var house = new House();
原文地址:https://www.cnblogs.com/littlewriter/p/7130380.html