This article mainly introduces the js simple factory mode usage. The example analyzes the implementation principle of the javascript factory mode. if you need it, you can refer to the example below to describe the js simple factory mode usage. Share it with you for your reference. The specific implementation method is as follows:
Simple factory modeScript // simple factory model var BicycleShop = function () {}; BicycleShop. prototype = {sellBicycle: function (model) {var bicycle = null; switch (model) {case 'The speedster': bicycle = new Speedster (); break; case 'The lowride': bicycle = new lowride (); break; case 'the Comfort Cruise ': bicycle = new ComfortCruise (); break;}; Interface. ensureImplements (bicycle, Bicycle); bicycle. assemble (); bicycle. wash (); return bicycle ;}}; var AcmeBicycleShop = function () {}; extent (AcmeBicycleShop, BicycleShop); AcmeBicycleShop. prototype. createBicycle = function (model) {var bicycle = null; switch (model) {case '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 ;}; // The Factory mode is applicable to the scenario where a fn creates different objects based on different parameters. script
I hope this article will help you design javascript programs.