蚂蚁论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 50|回复: 0

JavaScript 设计模式学习 Factory

[复制链接]

1979

主题

1

好友

1609

积分

管理员

Rank: 64Rank: 64Rank: 64Rank: 64

  • TA的每日心情
    奋斗
    2016-1-22 16:23
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    荣誉管理 论坛元老 热心会员 活跃会员 最佳新人 灌水之王

    发表于 2017-10-13 06:56:59 |显示全部楼层
    JavaScript 设计模式学习 Factory
    复制代码 代码如下: /* DisplayModule interface */ var DisplayModule = new Interface('DisplayModule', ['append', 'remove', 'clear']); /* ListDisplay class */ //通过接口实现工厂,这是通过List方式显示RSS var ListDisplay = function(id, parent) { // implements DisplayModule thislist = documentcreateElement('ul'); thislistid = id; parentappendChild(thislist); }; ListDisplayprototype = { append: function(text) { var newEl = documentcreateElement('li'); thislistappendChild(newEl); newElinnerHTML = text; return newEl; }, remove: function(el) { thislistremoveChild(el); }, clear: function() { thislistinnerHTML = ''; } }; /* Configuration object */ var conf = { id: 'cnn-top-stories', feedUrl: 'rsscnn/rss/cnn_topstoriesrss', updateInterval: 60, // In seconds parent: $('feed-readers') }; /* FeedReader class */ var FeedReader = function(display, xhrHandler, conf) { thisdisplay = display; thisxhrHandler = xhrHandler; thisconf = conf; thisstartUpdates(); }; FeedReaderprototype = { fetchFeed: function() { var that = this; var callback = { success: function(text, xml) { thatparseFeed(text, xml); }, failure: function(status) { thatshowError(status); } }; thisxhrHandlerrequest('GET', 'feedProxyphp?feed=' + thisconffeedUrl, callback); }, parseFeed: function(responseText, responseXML) { thisdisplayclear(); var items = responseXMLgetElementsByTagName('item'); for(var i = 0, len = itemslength; i lt; len; i++) { var title = items[i]getElementsByTagName('title')[0]; var link = items[i]getElementsByTagName('link')[0]; thisdisplayappend('lt;a href="' + linkfirstChilddata + '"gt;' + titlefirstChilddata + 'lt;/agt;'); } }, showError: function(statusCode) { thisdisplayclear(); thisdisplayappend('Error fetching feed'); }, stopUpdates: function() { clearInterval(thisinterval); }, startUpdates: function() { thisfetchFeed(); var that = this; thisinterval = setInterval(function() { thatfetchFeed(); }, thisconfupdateInterval * 1000); } }; /* FeedManager namespace */ //工厂管理器,这里可以根据传进来的参数选择具体的Display var FeedManager = { createFeedReader: function(conf) { var displayModule = new ListDisplay(confid + '-display', confparent); InterfaceensureImplements(displayModule, DisplayModule); var xhrHandler = XhrManagercreateXhrHandler(); InterfaceensureImplements(xhrHandler, AjaxHandler); return new FeedReader(displayModule, xhrHandler, conf); } }; ===================================================== 另一个自行车工厂的例子: var BicycleShop = function() {}; BicycleShopprototype = { sellBicycle: function(model) { var bicycle = thiscreateBicycle(model); bicycleassemble(); bicyclewash(); return bicycle; }, createBicycle: function(model) { throw new Error('Unsupported operation on an abstract class'); } }; /* AcmeBicycleShop class */ var AcmeBicycleShop = function() {}; extend(AcmeBicycleShop, BicycleShop); AcmeBicycleShopprototypecreateBicycle = 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 **fort Cruiser': default: bicycle = new Acme**fortCruiser(); } InterfaceensureImplements(bicycle, Bicycle); return bicycle; }; /* GeneralProductsBicycleShop class */ var GeneralProductsBicycleShop = function() {}; extend(GeneralProductsBicycleShop, BicycleShop); GeneralProductsBicycleShopprototypecreateBicycle = 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 **fort Cruiser': default: bicycle = new GeneralProducts**fortCruiser(); } InterfaceensureImplements(bicycle, Bicycle); return bicycle; };  /* Usage */ 复制代码 代码如下: var alecsCruisers = new AcmeBicycleShop(); var yourNewBike = alecsCruiserssellBicycle('The Lowrider'); var bobsCruisers = new GeneralProductsBicycleShop(); var yourSecondNewBike = bobsCruiserssellBicycle('The Lowrider');
    您需要登录后才可以回帖 登录 | 立即注册

    关闭

    蚂蚁站长论坛推荐上一条 /1 下一条

    QQ|Archiver|手机版|广告服务|蚂蚁站长-站长之家 ( 黔ICP备16008478号-1  

    GMT+8, 2017-10-23 06:50

    Powered by 站长论坛

    © 2007-2013 Jzant.com Inc.

    回顶部