Created
January 9, 2018 19:48
-
-
Save sinovic/ae9a2da7311934578c64961eeaf6a392 to your computer and use it in GitHub Desktop.
The factory pattern // source http://jsbin.com/xayekob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>The factory pattern</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/*A common implementation of this pattern is usually using | |
a class or static method of a class. | |
The purposes of such a class or method are as follows: | |
- It abstracts out repetitive operations when creating | |
similar objects | |
- It allows the consumers of the factory to create | |
objects without knowing the internals of the object creation | |
*/ | |
//Car Factory Constructor | |
function CarFactory() {}; | |
CarFactory.prototype.info = function() { | |
console.log('This car has ' + this.doors + 'doors and a ' + this.engine_capacity + 'liter engine'); | |
}; | |
// The static Factory Method | |
CarFactory.make = function (type) { | |
var constr = type, car; | |
CarFactory[constr].prototype = new CarFactory(); | |
// Create a new instance | |
car = new CarFactory[constr](); | |
return car; | |
}; | |
CarFactory.Compact = function () { | |
this.doors = 4; | |
this.engine_capacity = 2; | |
} | |
var golf = CarFactory.make('Compact'); | |
console.log(golf); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/*A common implementation of this pattern is usually using | |
a class or static method of a class. | |
The purposes of such a class or method are as follows: | |
- It abstracts out repetitive operations when creating | |
similar objects | |
- It allows the consumers of the factory to create | |
objects without knowing the internals of the object creation | |
*/ | |
//Car Factory Constructor | |
function CarFactory() {}; | |
CarFactory.prototype.info = function() { | |
console.log('This car has ' + this.doors + 'doors and a ' + this.engine_capacity + 'liter engine'); | |
}; | |
// The static Factory Method | |
CarFactory.make = function (type) { | |
var constr = type, car; | |
CarFactory[constr].prototype = new CarFactory(); | |
// Create a new instance | |
car = new CarFactory[constr](); | |
return car; | |
}; | |
CarFactory.Compact = function () { | |
this.doors = 4; | |
this.engine_capacity = 2; | |
} | |
var golf = CarFactory.make('Compact'); | |
console.log(golf); | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*A common implementation of this pattern is usually using | |
a class or static method of a class. | |
The purposes of such a class or method are as follows: | |
- It abstracts out repetitive operations when creating | |
similar objects | |
- It allows the consumers of the factory to create | |
objects without knowing the internals of the object creation | |
*/ | |
//Car Factory Constructor | |
function CarFactory() {}; | |
CarFactory.prototype.info = function() { | |
console.log('This car has ' + this.doors + 'doors and a ' + this.engine_capacity + 'liter engine'); | |
}; | |
// The static Factory Method | |
CarFactory.make = function (type) { | |
var constr = type, car; | |
CarFactory[constr].prototype = new CarFactory(); | |
// Create a new instance | |
car = new CarFactory[constr](); | |
return car; | |
}; | |
CarFactory.Compact = function () { | |
this.doors = 4; | |
this.engine_capacity = 2; | |
} | |
var golf = CarFactory.make('Compact'); | |
console.log(golf); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment