js简单工厂模式用法实例
本文实例讲述了js简单工厂模式用法。分享给大家供大家参考。具体实现方法如下:
<!DOCTYPEhtml> <html> <head> <title>简单工厂模式</title> </head> <body> <script> //简单工厂模式 varBicycleShop=function(){}; BicycleShop.prototype={ sellBicycle:function(model){ varbicycle=null; switch(model){ case'TheSpeedster': bicycle=newSpeedster(); break; case'Thelowride': bicycle=newLowride(); break; case'TheComfortCruise': bicycle=newComfortCruise(); break; }; Interface.ensureImplements(bicycle,Bicycle); bicycle.assemble(); bicycle.wash(); returnbicycle; } }; varAcmeBicycleShop=function(){}; extent(AcmeBicycleShop,BicycleShop); AcmeBicycleShop.prototype.createBicycle=function(model){ varbicycle=null; switch(model){ case'Thespeedster': bicycle=newAcmeSpeedster(); break; case'TheLowrider': bicycle=newAcmeLowrider(); break; case'TheFlatlander': bicycle=newAcmeFlatlander(); break; case'TheComfortCruiser': default: bicycle=newAcmeComfortCruiser(); }; Interface.ensureImplements(bicycle,Bicycle); returnbicycle; }; //工厂模式适用与一个fn根据参数不同,创建不同的对象 </script> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。