js simple factory pattern usage instance
- 2020-06-22 23:46:01
- OfStack
This example illustrates the use of the js simple factory pattern. Share to everybody for everybody reference. Specific implementation methods are as follows:
<!DOCTYPE html>
<html>
<head>
<title> Simple factory model </title>
</head>
<body>
<script>
// 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 '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;
};
// The factory model applies to 1 a fn Create different objects depending on the parameters
</script>
</body>
</html>
Hopefully, this article has been helpful in your javascript programming.