"Simple Factory mode": Same type
varCat =function () { This. hh = ' High cold '} cat.prototype={pin:function() {Console.log (White); } } varDog =function () { This. hh = ' sticky person '} dog.prototype={pin:function() {Console.log (Yellow); } } varren =function(PET) {Switch(PET) { Case' Qinjin ': return NewDog (); Case' Gaoleng ': return NewCat (); } } varGuke =Ren (qinjin) console.log (Guke);functionCat (name,pinzhong,price) {varo =NewObject (); O.name=name; O.pinzhong=Pinzhong O.price=Price O.getname=function() {Console.log ( This. Name); } returno;}varMeiduan = Cat (' Xiaoduanduan ', ' Meiduan ', 2000)varJiafei = Cat (' Xiaofeifei ', ' Feifei ', 9000) Meiduan.getname (); Jiafei.getname ()
"Factory method Mode"
var Factory = function (type,content) { if (this instanceof Factory) { var s = new This[type] (content) return s< c5/>}else{ return new Factory (type, content) } } factory.prototype = { dog:function (content) { c11/>this.content = content; (function (content) { var div = dosumnet.createelement (' div '); div.innerhtml = content; Div.style.border = ' 1px solid red ' document.getElementById (' Conta ')} ) (content) }, lv:function (content) { } } var data = [ {type: ' dog ', content: ' I am a Dog '}, {type: ' LV ', content: ' I am a dog '} ] For (var i = 2; i>0;i--) { Factory (s[i].type,s[i].content) }
"Abstract Factory mode"
Abstract Factory mode Each subclass has an inheritance of var home = function (Subtype,supertype) {//To determine if there is an object class in the abstract factory if (typeof home[supertype] = = = Fu Nction ') {//Cache class function F () {this.type= ' type '}//Inherit parent class properties and methods F.prototype = new Home[supertype]; The self-ray constructor is pointed to subclass Subtype.constructor = subtype//Subclass prototype Inherits "Parent class" Subtype.prototype = new F (); }else{//Does not exist this abstract class throws an error throw new Errow (' did not create this abstract class ')}}//Villa class Home.villa = function () {This.type = ' VI Lla ' Console.log (' car ', this);} Home.villa.prototype = {getprice:function () {}, Gethuxing:function () {}}//low house var Swimvilla = function (mianji,huxing) {This.mianji = Mianji; this.huxing = huxing;} Abstract factory implements inheritance of Villa abstract class home (Swimvilla, ' Villa '); swimvilla.prototype.getPrice = function () {Console.log (This.price)}SW imvilla.prototype.gethuxing = function () {console.log (this.huxing);} Const SWIMVILLA1 = new Swimvill (100, ' 5 Room ') swimvilla1.getPrice ()
The Factory mode of the JavaScript JavaScript design pattern