Permalink
Browse files

add factory method pattern

  • Loading branch information...
1 parent 60b2c84 commit 2bf6f401a8e484204043a90a1463bfbc6ca4b593 @icecreamliker committed Mar 8, 2012
Showing with 57 additions and 0 deletions.
  1. +20 −0 design_pattern/factory/index.htm
  2. +37 −0 design_pattern/factory/index.js
@@ -0,0 +1,20 @@
+<!doctype html>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>factory method pattern</title>
+ <script src="index.js" ></script>
+ </head>
+ <body>
+ <div>you can fulfill the form with 'dog', 'cat':</div>
+ <input id="J-value" type="text" />
+ <input onclick="trigger()" type="submit" value="click" />
+ <script>
+ function trigger(){
+ var newObj = PregnantCreator(document.getElementById('J-value').value);
+ newObj();//execute
+ }
+
+ </script>
+ </body>
+</html>
@@ -0,0 +1,37 @@
+/*
+ * yaoli<yaoli111144@gmail.com>
+ * 2012-3-8
+ * factory method pattern
+ */
+
+var Factory = function(){};
+Factory.prototype = {
+ constructor: Factory,
+ Dog : function(){
+ alert('dog is crying');
+ },
+ Cat : function(){
+ alert('cat is crying');
+ },
+ Animal : function(){
+ alert('default animal is crying');
+ }
+}
+ function PregnantCreator(type){
+ var innerObj;
+ switch(type){
+ case 'dog':
+ innerObj = new Factory().Dog;
+ break;
+ case 'cat':
+ innerObj = new Factory().Cat;
+ break;
+ default:
+ innerObj = new Factory().Animal;
+ }
+ return innerObj;
+ }
+
+ //test
+ //var newObj = PregnantCreator('dog');
+ //newObj();

0 comments on commit 2bf6f40

Please sign in to comment.