Skip to content

Commit cdfc5a5

Browse files
committed
added command pattern
1 parent db9d611 commit cdfc5a5

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

design-patterns/command.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
(function () {
10+
11+
var CarManager = {
12+
13+
/* request information */
14+
requestInfo: function(model, id) {
15+
return 'The purchase info for ' + model + ' with ID ' + id + ' is being processed...';
16+
},
17+
18+
/* purchase the car */
19+
buyVehicle: function(model, id) {
20+
return 'You have successfully purchased Item ' + id + ', a ' + model + '.';
21+
}
22+
23+
};
24+
25+
CarManager.execute = function (commad) {
26+
return CarManager[commad.request](commad.model,commad.carID);
27+
};
28+
29+
var actionA = CarManager.execute({request: "requestInfo", model: 'Ford Mondeo', carID: '543434'});
30+
console.log(actionA);
31+
var actionB = CarManager.execute({request: "buyVehicle", model: 'Ford Mondeo', carID: '543434'});
32+
console.log(actionB);
33+
34+
})();
35+
36+
</script>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)