JavaScript Design Pattern Learning factory_javascript Skills

Source: Internet
Author: User
Tags extend
Copy Code code as follows:

/* Displaymodule interface. */
var displaymodule = new Interface (' Displaymodule ', [' Append ', ' remove ', ' clear ']);
/* Listdisplay class. */
Implements the factory through the interface, this is the list way to display the RSS
var listdisplay = function (ID, parent) {//Implements Displaymodule
This.list = document.createelement (' ul ');
This.list.id = ID;
Parent.appendchild (this.list);
};
Listdisplay.prototype = {
Append:function (text) {
var newel = document.createelement (' li ');
This.list.appendChild (NEWEL);
newel.innerhtml = text;
return newel;
},
Remove:function (EL) {
This.list.removeChild (EL);
},
Clear:function () {
This.list.innerHTML = ';
}
};
/* Configuration object. */
var conf = {
ID: ' Cnn-top-stories ',
Feedurl: ' Http://rss.cnn.com/rss/cnn_topstories.rss ',
UPDATEINTERVAL:60,//in seconds.
Parent: $ (' feed-readers ')
};
/* Feedreader class. */
var feedreader = function (display, Xhrhandler, conf) {
This.display = display;
This.xhrhandler = Xhrhandler;
this.conf = conf;
This.startupdates ();
};
Feedreader.prototype = {
Fetchfeed:function () {
var that = this;
var callback = {
Success:function (text, XML) {that.parsefeed (text, XML);},
Failure:function (status) {That.showerror (status);}
};
This.xhrHandler.request (' Get ', ' feedproxy.php?feed= ' + THIS.CONF.FEEDURL,
callback);
},
Parsefeed:function (ResponseText, Responsexml) {
This.display.clear ();
var items = responsexml.getelementsbytagname (' item ');
for (var i = 0, len = items.length i < len; i++) {
var title = Items[i].getelementsbytagname (' title ') [0];
var link = items[i].getelementsbytagname (' link ') [0];
This.display.append (' <a href= ' + link.firstChild.data + ' "> ' +
Title.firstChild.data + ' </a> ');
}
},
Showerror:function (StatusCode) {
This.display.clear ();
This.display.append (' Error fetching feed. ');
},
Stopupdates:function () {
Clearinterval (This.interval);
},
Startupdates:function () {
This.fetchfeed ();
var that = this;
This.interval = setinterval (function () {that.fetchfeed ();},
This.conf.updateInterval * 1000);
}
};
/* Feedmanager namespace. */
Factory Manager, where you can select specific display based on the parameters that are passed in
var Feedmanager = {
Createfeedreader:function (conf) {
var displaymodule = new Listdisplay (conf.id + '-display ', conf.parent);
Interface.ensureimplements (Displaymodule, displaymodule);
var xhrhandler = Xhrmanager.createxhrhandler ();
Interface.ensureimplements (Xhrhandler, Ajaxhandler);
Return to New Feedreader (Displaymodule, Xhrhandler, conf);
}
};
=====================================================
Another example of a bicycle factory:
var bicycleshop = function () {};
Bicycleshop.prototype = {
Sellbicycle:function (model) {
var bicycle = this.createbicycle (model);
Bicycle.assemble ();
Bicycle.wash ();
return bicycle;
},
Createbicycle:function (model) {
throw new Error (' unsupported operation on an abstract class. ');
}
};
/* Acmebicycleshop class. */
var acmebicycleshop = function () {};
Extend (Acmebicycleshop, bicycleshop);
AcmeBicycleShop.prototype.createBicycle = function (model) {
var bicycle;
Switch (model) {
Case ' The Speedster ':
Bicycle = new Acmespeedster ();
Break
Case ' The Lowrider ':
Bicycle = new Acmelowrider ();
Break
Case ' The Flatlander ':
Bicycle = new Acmeflatlander ();
Break
Case ' The Comfort Cruiser ':
Default
Bicycle = new Acmecomfortcruiser ();
}
Interface.ensureimplements (bicycle, bicycle);
return bicycle;
};
/* Generalproductsbicycleshop class. */
var generalproductsbicycleshop = function () {};
Extend (Generalproductsbicycleshop, bicycleshop);
GeneralProductsBicycleShop.prototype.createBicycle = function (model) {
var bicycle;
Switch (model) {
Case ' The Speedster ':
Bicycle = new Generalproductsspeedster ();
Break
Case ' The Lowrider ':
Bicycle = new Generalproductslowrider ();
Break
Case ' The Flatlander ':
Bicycle = new Generalproductsflatlander ();
Break
Case ' The Comfort Cruiser ':
Default
Bicycle = new Generalproductscomfortcruiser ();
}
Interface.ensureimplements (bicycle, bicycle);
return bicycle;
};

/* Usage. */
Copy Code code as follows:

var alecscruisers = new Acmebicycleshop ();
var yournewbike = alecscruisers.sellbicycle (' the Lowrider ');
var bobscruisers = new Generalproductsbicycleshop ();
var yoursecondnewbike = bobscruisers.sellbicycle (' the Lowrider ');

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.