# 组合模式
- 描述:将多个部分通过组合变成一个整体。
- 好处:
- 例子:
{/*
<form>
<div class="form-line">
<label for="user">用户名</label>
<input type="text" id="user" name="user">
</div>
<div class="form-line">
<label for="pwd">密码</label>
<input type="password" id="pwd" name="pwd">
</div>
<div class="form-line">
<input type="submit" value="登录">
</div>
</form>
*/}
// 使用组合模式实现以上的
window.onload = function() {
// 组合寄生式继承
function inheritPrototype(subClass, superClass) {
function F() {}
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
}
// 基类
function Container() {
this.children = [];
this.element = null;
}
Container.prototype = {
init: function() {
throw new Error("重写init方法");
},
add: function(child) {
this.children.push(child);
this.element.appendChild(child.element);
},
};
// 基于容器基类创建表单容器
function CreateForm(id, method, action, parent) {
Container.call(this);
this.id = id || "";
this.method = method || "get";
this.action = action || "";
this.parent = parent;
this.init();
}
inheritPrototype(CreateForm, Container);
CreateForm.prototype.init = function() {
this.element = document.createElement("form");
this.element.id = this.id;
this.element.method = this.method;
this.element.action = this.action;
};
CreateForm.prototype.show = function() {
this.parent.appendChild(this.element);
};
// 行容器组件
function CreateLine(className) {
Container.call(this);
this.className =
className === undefined ? "form-line" : "form-line" + className;
this.init();
}
inheritPrototype(CreateLine, Container);
CreateLine.prototype.init = function() {
this.element = document.createElement("div");
this.element.className = this.className;
};
// label组件
function CreateLable(text, forName) {
this.text = text || "";
this.forName = forName || "";
this.init();
}
CreateLable.prototype.init = function() {
this.element = document.createElement("label");
this.element.setAttribute("for", this.forName);
this.element.innerHTML = this.text;
};
// input组件
function CreateInput(type, id, name, defaultValue) {
this.type = type || "";
this.id = id || "";
this.name = name || "";
this.defaultValue = defaultValue || "";
this.init();
}
CreateInput.prototype.init = function() {
this.element = document.createElement("input");
this.element.type = this.type;
this.element.id = this.id;
this.element.name = this.name;
this.element.defaultValue = this.defaultValue;
};
// 使用
var form = new CreateForm("ownerForm", "GET", "/aaa.html", document.body);
var useLine = new CreateLine();
useLine.add(new CreateLable("用户名", "user"));
useLine.add(new CreateInput("text", "user", "user"));
var pwdLine = new CreateLine();
pwdLine.add(new CreateLable("密码", "pwd"));
pwdLine.add(new CreateInput("password", "pwd", "pwd"));
var submitLine = new CreateLine();
submitLine.add(new CreateInput("submit", "", "", "登录"));
form.add(useLine);
form.add(pwdLine);
form.add(submitLine);
form.show();
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121