The Factory mode of the JavaScript JavaScript design pattern

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.