简单工厂模式:
1: 创建对象,不同类的实例化进行合并处理
// 篮球基类
var Basketball = function () {
this.intro = '篮球盛行于美国';
};
Basketball.prototype = {
getMember: function () {
console.log("每个队伍需要5名队员");
},
getBallSize: function () {
console.log("篮球很大");
}
};
// 足球基类
var Football = function () {
this.intro = '足球盛行于很多国家';
};
Football.prototype = {
getMember: function () {
console.log("每个队伍需要11名队员");
},
getBallSize: function () {
console.log("足球很大");
}
};
// 网球基类
var Tennis = function () {
this.intro = '每年都有网球比赛';
};
Tennis.prototype = {
getMember: function () {
console.log("每个队伍需要1名队员");
},
getBallSize: function () {
console.log("网球很大");
}
};
// 运动工厂
var SportFactory = function ( name ) {
switch( name ){
case 'NBA':
return new Basketball();
case 'wordCup':
return new Football();
case 'tennis':
return new Tennis();
}
};
var fball = SportFactory("wordCup");
console.log( fball );
console.log( fball.intro );
fball.getMember();
2 . 提取公有属性,方法,例如书籍,提取名字,类别,方法等。
如果有不同点,则进行单独处理
function createBook( name , type ){
var o = new Object();
o.name = name;
o.type = type;
o.getName = function () {
console.log( this.name );
};
// 如果有特殊点,可以在这里补充,比如 该本书的出版人啦
if( name.indexOf("js") > -1 ){
o.publishMan = "anyOne";
}
return o;
}
var book1 = createBook("css book" , "css" );
var book2 = createBook('js book' , 'js' );
book1.getName();
console.log( book2.publishMan );
简单工厂 ----> 工厂模式 , 废话不多说,直接调代码
// 简单工厂-> 工厂模式
// 创建java 学科类
var Java = function (con) {
this.content = con;
// 通过闭包直接执行
(function () {
var div = document.createElement('div');
div.innerHTML = con;
div.style.color = 'green';var attr = document.createAttribute("id");
attr.nodeValue = "haha"+con;
div.setAttributeNode( attr );
document.body.appendChild(div);
})( con );
};
// 创建php 学科类
var Php = function (con) {
this.content = con;
// 通过闭包直接执行
(function () {
var div = document.createElement('div');
div.innerHTML = con;
div.style.color = 'yellow';
div.style.background = 'red';
var attr = document.createAttribute("id");
attr.nodeValue = "hehe"+con;
div.setAttributeNode( attr );
document.body.appendChild(div);
})( con );
};
// 创建学科类工厂
function JobFactory( type , con ){
switch (type) {
case 'java':
return new Java( con );
case 'php':
return new Php( con );
default :
throw new Error("do not have this type!");
break;
}
}
JobFactory( 'java' , 'java是世界最好的编程语言');
JobFactory( 'php' , 'php是世界最好的编程语言');
JobFactory( 'php' , 'php是狗屎');
JobFactory( 'java' , 'java是狗屎');
// 工厂模式, 私以为是把工厂模式创建的诸多类,还要在工厂里添加switch, 为了方便,直接在工厂的原型里写就可以了,工厂升级。
// 安全模式,先判断一下使用时是否用new 创建了
var Factory = function ( type , con ) {
// 判断this指代的是不是 Factory 类 ,如果是则是通过new创建的
if( this instanceof Factory ){
var s = new this[type]( con );
return s;
}else{
return new Factory( type , con );
}
};
Factory.prototype = {
Java : function ( con ) {
this.content = con;
// 通过闭包直接执行
(function () {
var div = document.createElement('div');
div.innerHTML = con;
div.style.color = 'green';var attr = document.createAttribute("id");
attr.nodeValue = "haha"+con;
div.setAttributeNode( attr );
document.body.appendChild(div);
})( con );
},
Php : function ( con ) {
this.content = con;
// 通过闭包直接执行
(function () {
var div = document.createElement('div');
div.innerHTML = con;
div.style.color = 'yellow';
div.style.background = 'red';
var attr = document.createAttribute("id");
attr.nodeValue = "hehe"+con;
div.setAttributeNode( attr );
document.body.appendChild(div);
})( con );
}
};
Factory('Java' , '学过一段时间,没写过' );
Factory('Php' , '代码可以看懂,但是没写过' );