목적
객체 생성에 관련된 패턴입니다.
객체를 생성하기 위한 인터페이스를 정의하고, 인스턴스 생성은 서브클래스가 결정하게 한다
구조
샘플 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   | function Factory() {     this.createEmployee = function (type) {         var employee;           if (type === "fulltime") {             employee = new FullTime();         } else if (type === "parttime") {             employee = new PartTime();         } else if (type === "temporary") {             employee = new Temporary();         } else if (type === "contractor") {             employee = new Contractor();         }           employee.type = type;           employee.say = function () {             log.add(this.type + ": rate " + this.hourly + "/hour");         }           return employee;     } }   var FullTime = function () {     this.hourly = "$12"; };   var PartTime = function () {     this.hourly = "$11"; };   var Temporary = function () {     this.hourly = "$10"; };   var Contractor = function () {     this.hourly = "$15"; };  
  var log = (function () {     var log = "";       return {         add: function (msg) { log += msg + "\n"; },         show: function () { alert(log); log = ""; }     } })();   function run() {     var employees = [];     var factory = new Factory();       employees.push(factory.createEmployee("fulltime"));     employees.push(factory.createEmployee("parttime"));     employees.push(factory.createEmployee("temporary"));     employees.push(factory.createEmployee("contractor"));          for (var i = 0, len = employees.length; i < len; i++) {         employees[i].say();     }       log.show(); }
  |