javascript 简单工厂模式

			var Bicycle = new Interface("Bicycle",["assemble","wash","ride","repair"]);
			var Lowride = function() {};
			Lowride.prototype = {
				assemble: function() {
					document.write("Lowride assemble success!<br />")
				},
				wash: function() {
					document.write("Lowride wash success!<br />");
				},
				ride: function() {
					document.write("Lowride ride success!<br />");
				},
				repair: function() {
					document.write("Lowride repair success!<br />");
				}
			};
			var Speedstr = function() {};
			Speedstr.prototype = {
				assemble: function() {
					document.write("Speedstr assemble success!<br />")
				},
				wash: function() {
					document.write("Speedstr wash success!<br />");
				},
				ride: function() {
					document.write("Speedstr ride success!<br />");
				},
				repair: function() {
					document.write("Speedstr repair success!<br />");
				}
			}
			var Comfortcruisor = function() {};
			Comfortcruisor.prototype = {
				assemble: function() {
					document.write("Comfortcruisor assemble success!<br />")
				},
				wash: function() {
					document.write("Comfortcruisor wash success!<br />");
				},
				ride: function() {
					document.write("Comfortcruisor ride success!<br />");
				},
				repair: function() {
					document.write("Comfortcruisor repair success!<br />");
				}
			}
			var BicycleFactory = function() {};
			BicycleFactory.prototype = {
				createBicycle: function(model) {
					var bicycle;
					switch(model){
						case "Lowride":
							bicycle = new Lowride();
							break;
						case "Speedstr":
							bicycle = new Speedstr();
							break;
						case "Comfortcruisor":
							bicycle = new Comfortcruisor();
							break;
					}
					Interface.ensureImplents(bicycle,Bicycle);
					return bicycle;
				}
			}
			var BicycleShop = new BicycleFactory();
			var concertBicycle = BicycleShop.createBicycle("Speedstr");
			concertBicycle.assemble();
			concertBicycle.wash();
			concertBicycle.ride();
			concertBicycle.repair();

  

你可能感兴趣的:(JavaScript)