Skip to content
This repository was archived by the owner on Mar 1, 2021. It is now read-only.

Commit 457b924

Browse files
committed
Add factory examples
1 parent fc6a4eb commit 457b924

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Factory: Abstract Factory</title>
6+
<meta name="description" content="">
7+
<meta name="author" content="">
8+
</head>
9+
<body>
10+
<script type="text/javascript" src="main.js"></script>
11+
</body>
12+
</html>

factory/abstract-factory/main.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"use strict";
2+
3+
// Polyfill -- Only necessary for browsers which don't support Object.create. Check this ES5 compatibility table:
4+
// http://kangax.github.com/es5-compat-table/
5+
if (!Object.create) {
6+
Object.create = function (o) {
7+
if (arguments.length > 1) {
8+
throw new Error('Object.create implementation only accepts the first parameter.');
9+
}
10+
function F() {}
11+
F.prototype = o;
12+
return new F();
13+
};
14+
}
15+
16+
17+
// Credit to Yehuda Katz for `fromPrototype` function
18+
// http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
19+
var fromPrototype = function(prototype, object) {
20+
var newObject = Object.create(prototype);
21+
for (var prop in object) {
22+
if (object.hasOwnProperty(prop)) {
23+
newObject[prop] = object[prop];
24+
}
25+
}
26+
return newObject;
27+
};
28+
29+
30+
// Define our `Ingredients` base object
31+
var Ingredients = {
32+
createDough: function() {
33+
return 'generic dough';
34+
},
35+
createSauce: function() {
36+
return 'generic sauce';
37+
},
38+
createCrust: function() {
39+
return 'generic crust';
40+
}
41+
};
42+
43+
// Extend `Ingredients` with concrete implementations
44+
Ingredients.createChicagoStyle = function() {
45+
return fromPrototype(Ingredients, {
46+
createDough: function() {
47+
return 'thick, heavy dough';
48+
},
49+
createSauce: function() {
50+
return 'rich marinara';
51+
},
52+
createCrust: function() {
53+
return 'deep-dish';
54+
}
55+
});
56+
};
57+
58+
Ingredients.createCaliforniaStyle = function() {
59+
return fromPrototype(Ingredients, {
60+
createDough: function() {
61+
return 'light, fluffy dough';
62+
},
63+
createSauce: function() {
64+
return 'tangy red sauce';
65+
},
66+
createCrust: function() {
67+
return 'thin and crispy';
68+
}
69+
});
70+
};
71+
72+
// Try it out!
73+
var californiaIngredients = Ingredients.createCaliforniaStyle();
74+
console.log(californiaIngredients.createCrust()); // returns 'thin and crispy';

factory/factory-method/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Factory: Factory Method</title>
6+
<meta name="description" content="">
7+
<meta name="author" content="">
8+
</head>
9+
<body>
10+
<script type="text/javascript" src="main.js"></script>
11+
</body>
12+
</html>

factory/factory-method/main.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"use strict";
2+
3+
// Polyfill -- Only necessary for browsers which don't support Object.create. Check this ES5 compatibility table:
4+
// http://kangax.github.com/es5-compat-table/
5+
if (!Object.create) {
6+
Object.create = function (o) {
7+
if (arguments.length > 1) {
8+
throw new Error('Object.create implementation only accepts the first parameter.');
9+
}
10+
function F() {}
11+
F.prototype = o;
12+
return new F();
13+
};
14+
}
15+
16+
17+
// Credit to Yehuda Katz for `fromPrototype` function
18+
// http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
19+
var fromPrototype = function(prototype, object) {
20+
var newObject = Object.create(prototype);
21+
for (var prop in object) {
22+
if (object.hasOwnProperty(prop)) {
23+
newObject[prop] = object[prop];
24+
}
25+
}
26+
return newObject;
27+
};
28+
29+
// Define the Pizza product
30+
var Pizza = {
31+
description: 'Plain Generic Pizza'
32+
};
33+
34+
// And the basic PizzaStore
35+
var PizzaStore = {
36+
createPizza: function(type) {
37+
if (type == 'cheese') {
38+
return fromPrototype(Pizza, {
39+
description: 'Cheesy, Generic Pizza'
40+
});
41+
} else if (type == 'veggie') {
42+
return fromPrototype(Pizza, {
43+
description: 'Veggie, Generic Pizza'
44+
});
45+
}
46+
}
47+
};
48+
49+
var ChicagoPizzaStore = fromPrototype(PizzaStore, {
50+
createPizza: function(type) {
51+
if (type == 'cheese') {
52+
return fromPrototype(Pizza, {
53+
description: 'Cheesy, Deep-dish Chicago Pizza'
54+
});
55+
} else if (type == 'veggie') {
56+
return fromPrototype(Pizza, {
57+
description: 'Veggie, Deep-dish Chicago Pizza'
58+
});
59+
}
60+
}
61+
});
62+
63+
var CaliforniaPizzaStore = fromPrototype(PizzaStore, {
64+
createPizza: function(type) {
65+
if (type == 'cheese') {
66+
return fromPrototype(Pizza, {
67+
description: 'Cheesy, Tasty California Pizza'
68+
});
69+
} else if (type == 'veggie') {
70+
return fromPrototype(Pizza, {
71+
description: 'Veggie, Tasty California Pizza'
72+
});
73+
}
74+
}
75+
});
76+
77+
// Elsewhere in our app...
78+
var chicagoStore = Object.create(ChicagoPizzaStore);
79+
var pizza = chicagoStore.createPizza('veggie');
80+
console.log(pizza.description); // returns 'Veggie, Deep-dish Chicago Pizza'

factory/simple-factory/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Factory: Simple Factory</title>
6+
<meta name="description" content="">
7+
<meta name="author" content="">
8+
</head>
9+
<body>
10+
<script type="text/javascript" src="main.js"></script>
11+
</body>
12+
</html>

factory/simple-factory/main.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
function Admin() {
4+
console.log('Admin created!');
5+
}
6+
7+
function Customer() {
8+
console.log('Customer created!');
9+
}
10+
11+
var UserFactory = {};
12+
UserFactory.createUser = function(type) {
13+
if (type == 'admin') {
14+
return new Admin();
15+
} else if (type == 'customer') {
16+
return new Customer();
17+
}
18+
};
19+
20+
var customer = UserFactory.createUser('admin');
21+
var customer = UserFactory.createUser('customer');

0 commit comments

Comments
 (0)