├── Abstract Factory.html
├── Adapter.html
├── Bridge.html
├── Builder.html
├── Chain of responsibility.html
├── Command.html
├── Composite.html
├── Decorator.html
├── Facade.html
├── Factory Method.html
├── Flyweight.html
├── Interpreter.html
├── Iterator.html
├── Mediator.html
├── Memento.html
├── Observer.html
├── Proxy.html
├── README.md
├── Simple JavaScript Inheritance.js
├── Singleton.html
├── State.html
├── Strategy.html
└── Template.html
/Abstract Factory.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 抽线工厂
6 |
7 |
8 |
9 |
231 |
232 |
233 |
--------------------------------------------------------------------------------
/Adapter.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
506 |
507 |
508 |
--------------------------------------------------------------------------------
/Bridge.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
601 |
602 |
603 |
--------------------------------------------------------------------------------
/Builder.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 生成器模式
6 |
7 |
8 |
9 |
405 |
406 |
407 |
--------------------------------------------------------------------------------
/Chain of responsibility.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 职责链模式
5 |
6 |
7 |
8 |
9 |
709 |
710 |
711 |
--------------------------------------------------------------------------------
/Composite.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
811 |
812 |
813 |
--------------------------------------------------------------------------------
/Facade.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
314 |
315 |
316 |
--------------------------------------------------------------------------------
/Mediator.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 中介者模式
6 |
7 |
8 |
38 |
671 |
672 |
673 |
--------------------------------------------------------------------------------
/Memento.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Memento
6 |
7 |
8 |
9 |
455 |
456 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | JavascriptDesignPatterns
2 | ========================
3 |
4 | Javascript design patterns
5 |
6 | some collected JS design patterns learning.
7 |
8 |
9 | Creational Patterns
10 | Abstract Factory Creates an instance of several families of classes
11 | Builder Separates object construction from its representation
12 | Factory Method Creates an instance of several derived classes
13 | Prototype A fully initialized instance to be copied or cloned
14 | Singleton A class of which only a single instance can exist
15 |
16 | Structural Patterns
17 | Adapter Match interfaces of different classes
18 | Bridge Separates an object’s interface from its implementation
19 | Composite A tree structure of simple and composite objects
20 | Add responsibilities to objects dynamically
21 | Facade A single class that represents an entire subsystem
22 | Flyweight A fine-grained instance used for efficient sharing
23 | Proxy An object representing another object
24 |
25 | Behavioral Patterns
26 | Chain of Resp. A way of passing a request between a chain of objects
27 | Command Encapsulate a command request as an object
28 | Interpreter A way to include language elements in a program
29 | Iterator Sequentially access the elements of a collection
30 | Mediator Defines simplified communication between classes
31 | Memento Capture and restore an object's internal state
32 | Observer A way of notifying change to a number of classes
33 | State Alter an object's behavior when its state changes
34 | Strategy Encapsulates an algorithm inside a class
35 | Template Method Defer the exact steps of an algorithm to a subclass
36 | Visitor Defines a new operation to a class without change
37 |
--------------------------------------------------------------------------------
/Simple JavaScript Inheritance.js:
--------------------------------------------------------------------------------
1 | /* Simple JavaScript Inheritance
2 | * By John Resig http://ejohn.org/
3 | * MIT Licensed.
4 | */
5 | (function () {
6 | /*
7 | fnTest是用来检测是否有_super方法名字符串
8 | */
9 | var initializing = false, fnTest = /xyz/.test(function () {
10 | xyz;
11 | }) ? /\b_super\b/ : /.*/;
12 | /*
13 | 先定义全局Class构造函数
14 | */
15 | // The base Class implementation (does nothing)
16 | this.Class = function () {
17 | };
18 |
19 | /*
20 | Class构造函数的静态方法,从这个Class extend的构造函数将会继承前者,并且带有extend静态方法,这样就可以不断继承
21 | */
22 | // Create a new Class that inherits from this class
23 | Class.extend = function (prop) {
24 | // this是指的对象,不是实例,
25 | // 第一次用Class.extend扩展时,
26 | // this === window.Class,
27 | // 当继承了这个的其它构造函数的extend方法中的this
28 | // 也就指向该构造函数对象了,与实例化无关
29 | var _super = this.prototype;
30 |
31 | // Instantiate a base class (but only create the instance,
32 | // don't run the init constructor)
33 | // 初始化中,因为设置了true,不会运行init方法
34 | initializing = true;
35 | // 实例化基础Class或者父类
36 | // 这里this就是个构造函数,因此可以实例化,
37 | // 此时prototype就是个对象了
38 | var prototype = new this();
39 | initializing = false;
40 |
41 | // Copy the properties over onto the new prototype
42 | for (var name in prop) {
43 | // Check if we're overwriting an existing function
44 | // 检查我们是否正在重写父类函数,
45 | // prototype是个实例,给实例添加方法不会被添加到构造函数中
46 | prototype[name] = typeof prop[name] == "function" &&
47 | typeof _super[name] == "function" && fnTest.test(prop[name]) ?
48 | (function (name, fn) {
49 | return function () {
50 | // tmp为空
51 | var tmp = this._super;
52 |
53 | // Add a new ._super() method that is the same method
54 | // but on the super-class
55 | // 将this._super保存为父类原型中的方法
56 | this._super = _super[name];
57 |
58 | // The method only need to be bound temporarily, so we
59 | // remove it when we're done executing
60 | // 执行fn函数,并且this的_super方法就是保存着父类的方法
61 | var ret = fn.apply(this, arguments);
62 | // 执行完后设成undefined
63 | this._super = tmp;
64 |
65 | return ret;
66 | };
67 | })(name, prop[name]) :
68 | prop[name];
69 | }
70 |
71 | // The dummy class constructor
72 | // 创建一个新的构造函数
73 | function Class() {
74 | // All construction is actually done in the init method
75 | if (!initializing && this.init)
76 | this.init.apply(this, arguments);
77 | }
78 |
79 | // Populate our constructed prototype object
80 | // 继承父类
81 | Class.prototype = prototype;
82 |
83 | // Enforce the constructor to be what we expect
84 | Class.constructor = Class;
85 |
86 | // And make this class extendable
87 | // 使其同样可扩展子类
88 | Class.extend = arguments.callee;
89 |
90 | // 返回该Class
91 | return Class;
92 | };
93 | })();
94 |
--------------------------------------------------------------------------------
/Singleton.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
423 |
424 |
425 |
--------------------------------------------------------------------------------
/State.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | State Pattern
6 |
7 |
8 |
9 |
567 |
568 |
569 |
--------------------------------------------------------------------------------
/Strategy.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
698 |
699 |
700 |
--------------------------------------------------------------------------------
/Template.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
737 |
738 |
739 |
--------------------------------------------------------------------------------