-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo4.js
101 lines (89 loc) · 2.78 KB
/
demo4.js
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
/**
* Created by Administrator on 2017/8/26 0026.
*/
//工厂方法模式---通过对产品类的抽象,使其创建业务员主要用于创建多累产品的实例
var Java = function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "green";
document.getElementById("container").appendChild(div)
})(content)
}
var Php = function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "red";
document.getElementById("container").appendChild(div)
})(content)
}
var Javascript = function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "pink";
document.getElementById("container").appendChild(div)
})(content)
}
//简单工厂模式
function JobFactory(type,content){
switch(type){
case 'java' :
return new Java(content);
case 'php' :
return new Php(content);
case 'javascript' :
return new Javascript(content)
}
}
//安全模式工厂方法
var Factory = function(type,content){
if( this instanceof Factory ){
var s = new this[type](content) //new加深了理解困难 这里返回的是实例的方法
return s;
}else{
return new Factory(type,content)
}
}
Factory.prototype = {
Java : function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "green";
document.getElementById("container").appendChild(div)
})(content)
},
Javascript : function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "pink";
document.getElementById("container").appendChild(div)
})(content)
},
php : function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "red";
document.getElementById("container").appendChild(div)
})(content)
},
ui : function(content){
this.content = content;
(function(content){
var div = document.createElement("div");
div.innerHTML = content;
div.style.color = "#ccc";
document.getElementById("container").appendChild(div)
})(content)
}
}