JavaScript设计模式-9.工厂模式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript高级语法9-工厂模式</title>
</head>
<body>
<script>
/*1.简单工厂 :通过第三方的类完成松耦合的任务 ->工厂。
* 2.复杂工厂:通过把实例化的任务交个子类来完成,用来达到松耦合的目的 ->工厂。
* 3.超级工厂:通过eval来完成智能工厂。
*/
/*工厂的目的在于判别接口最终用哪类来实例化、
* 产生实例的过程不用new关键字
* 最终达到的效果是多台,类与类之间的松耦合
*/ //需要用到的继承和接口方法
function extend(subClass,superClass){
//1.叫子类原型类属性等于父类的原型属性
//初始化一个中间空对象,为了转换主父类关系
var F = function(){};
F.prototype = superClass.prototype;
//2.让子类集成F
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
//3.为子类增加属性superClass
subClass.superClass = superClass.prototype;
//4.增加一个保险,就算你是的原型类是超类(Object) 那么也要把你的构造函数级别讲下来
if(superClass.prototype.constructor == Object.prototype.constructor){
superClass.prototype.constructor = superClass;
}
}
//接口
var Interface = function(name,methods){
if(arguments.length != 2){
alert("interface must have two paramters...");
}
this.name = name;//这个是接口的名字
this.methods = [];//定义个空数组来转载函数名
for (var i = 0; i < methods.length; i++) {
if(typeof methods[i] != "string"){
alert("method name must is String ...")
}else{
this.methods.push(methods[i])
}
}
}
//定义接口的一个静态方法来实现接口与实现类的直接检验
//静态方法不要写成Interface.prototype.* 因为这是写到接口原型连上的
//我们要把静态的函数直接写到类层次上
Interface.ensureImplements = function(object){
if(arguments.length<2){
alert("必须最少是2个参数");
return false;
}
//遍历
for (var i = 1; i < arguments.length; i++) {
var inter = arguments[i];
//如果你是接口就必须是Interface类型的
if(inter.constructor != Interface){
throw new Error("if is interface class must is Interface type");
}
//遍历函数集合并分析
for (var j = 0; j < inter.methods.length; j++) {
var method = inter.methods[j];
//实现类中必须有方法名字 和 接口中所有的方法名项目
if(!object[method] || typeof object[method] != "function"){
throw new Error("实现类并且没有完全实现接口中的所有方法...");
}
}
}
} function factory1(){
var Pet = new Interface("Pet",["eat","run","sing","register"]);
//宠物店
var PetShop = function(){}
PetShop.prototype = {
//出售宠物的方法
sellPetShop:function(kind){
//宠物对象
var pet;
//kind种类
switch(kind){
case 'dog':
pet = new Dog();
break;
case 'cat':
pet = new Cat();
break;
case 'pig':
pet = new Pig();
break;
default:
//鸟
pet = new Bird();
}
//验证接口
Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
return pet;
}
}
//宠物基类
function BasePet(){
this.register = function(){
document.write("宠物登记 。。。<br>")
}
this.eat = function(){
document.write("吃顿饱饭 <br>")
}
}
//实现
function Dog(){
Dog.superClass.constructor.call(this);
this.run = function(){
document.write("小狗跑步 。。<br>")
}
this.sing = function(){
document.write("小狗唱歌。。。<br>")
}
}
function Cat(){
Cat.superClass.constructor.call(this);
this.run = function(){
document.write("小猫跑步 。。<br>")
}
this.sing = function(){
document.write("小猫唱歌。。。<br>")
}
}
function Pig(){
Pig.superClass.constructor.call(this);
this.run = function(){
document.write("小猪跑步 。。<br>")
}
this.sing = function(){
document.write("小猪唱歌。。。<br>")
}
}
function Bird(){
Bird.superClass.constructor.call(this);
this.run = function(){
document.write("小鸟跑步 。。<br>")
}
this.sing = function(){
document.write("小鸟唱歌。。。<br>")
}
} //继承
extend(Dog,BasePet);
extend(Pig,BasePet);
extend(Bird,BasePet);
extend(Cat,BasePet); //pcat宠物店
var pcatPetShop = new PetShop();
var flowerPig = pcatPetShop.sellPetShop("pig");
flowerPig.run(); /*貌似很完美,但是他禁不住需求的变化
* 比如说宠物商店又进来一些新的品种宠物
* 这时候用目前的方法必须要修改宠物商店这个类
* 用一个简单工厂来解决
*/ }
// factory1(); function factory2(){
//静态工厂
var Pet = new Interface("Pet",["eat","run","sing","register"]);
var PetFactory = {
sellPetShop:function(kind){
//宠物对象
var pet;
//kind种类
switch(kind){
case 'dog':
pet = new Dog();
break;
case 'cat':
pet = new Cat();
break;
case 'pig':
pet = new Pig();
break;
default:
//鸟
pet = new Bird();
}
//验证接口
Interface.ensureImplements(pet,Pet);
return pet;
}
}
//利用工厂的新宠物店
var PetShop2 = function(){}
PetShop2.prototype = {
sellPetShop:function(kind){
var pet = PetFactory.sellPetShop(kind);
pet.eat();
pet.register();
return pet;
}
} //宠物基类
function BasePet(){
this.register = function(){
document.write("宠物登记 。。。<br>")
}
this.eat = function(){
document.write("吃顿饱饭 <br>")
}
}
//实现
function Dog(){
Dog.superClass.constructor.call(this);
this.run = function(){
document.write("小狗跑步 。。<br>")
}
this.sing = function(){
document.write("小狗唱歌。。。<br>")
}
}
function Cat(){
Cat.superClass.constructor.call(this);
this.run = function(){
document.write("小猫跑步 。。<br>")
}
this.sing = function(){
document.write("小猫唱歌。。。<br>")
}
}
function Pig(){
Pig.superClass.constructor.call(this);
this.run = function(){
document.write("小猪跑步 。。<br>")
}
this.sing = function(){
document.write("小猪唱歌。。。<br>")
}
}
function Bird(){
Bird.superClass.constructor.call(this);
this.run = function(){
document.write("小鸟跑步 。。<br>")
}
this.sing = function(){
document.write("小鸟唱歌。。。<br>")
}
} //继承
extend(Dog,BasePet);
extend(Pig,BasePet);
extend(Bird,BasePet);
extend(Cat,BasePet); var pcatPetShop2 = new PetShop2();
var flowerCat = pcatPetShop2.sellPetShop("cat");
flowerCat.sing(); /*貌似很完美
* 新的需求:都是宠物店
* 张三的店主要卖哈士奇,李四的店卖各种鸟
*/
} // factory2(); function factory3(){
var Pet = new Interface("Pet",["eat","run","sing","register"]);
//宠物店
var PetShop = function(){}
PetShop.prototype = {
//出售宠物的方法
sellPetShop:function(kind){
//宠物对象
var pet;
//kind种类
switch(kind){
case 'dog':
pet = new Dog();
break;
case 'cat':
pet = new Cat();
break;
case 'pig':
pet = new Pig();
break;
default:
//鸟
pet = new Bird();
}
//验证接口
Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
return pet;
}
}
//宠物基类
function BasePet(){
this.register = function(){
document.write("宠物登记 。。。<br>")
}
this.eat = function(){
document.write("吃顿饱饭 <br>")
}
}
//实现
function Dog(){
Dog.superClass.constructor.call(this);
this.run = function(){
document.write("小狗跑步 。。<br>")
}
this.sing = function(){
document.write("小狗唱歌。。。<br>")
}
}
function Cat(){
Cat.superClass.constructor.call(this);
this.run = function(){
document.write("小猫跑步 。。<br>")
}
this.sing = function(){
document.write("小猫唱歌。。。<br>")
}
}
function Pig(){
Pig.superClass.constructor.call(this);
this.run = function(){
document.write("小猪跑步 。。<br>")
}
this.sing = function(){
document.write("小猪唱歌。。。<br>")
}
}
function Bird(){
Bird.superClass.constructor.call(this);
this.run = function(){
document.write("小鸟跑步 。。<br>")
}
this.sing = function(){
document.write("小鸟唱歌。。。<br>")
}
} //继承
extend(Dog,BasePet);
extend(Pig,BasePet);
extend(Bird,BasePet);
extend(Cat,BasePet); //1.把核心的商店变成一个抽象类
var Petshop = function(){}
Petshop.prototype = {
sellPetShop:function(kind){
var pet = this.sellPetshop(kind);
pet.eat();
pet.register();
return pet;
},
sellPetshop:function(model){
throw new Error("this is abstract class")
}
}
//2.利用子类满足上边的需求(多态)
var OnePetShop = function(){}
extend(OnePetShop,Petshop);
OnePetShop.prototype.sellPetshop = function(model){
var pet;
//kind种类
switch(model){
case 'dog':
pet = new Dog();
break;
default:
//鸟
pet = new Bird();
}
//验证接口
Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
return pet;
} var TwoPetShop = function(){}
extend(TwoPetShop,Petshop);
TwoPetShop.prototype.sellPetshop = function(model){
var pet;
//kind种类
switch(model){
case 'pig':
pet = new Pig();
break;
default:
//鸟
pet = new Bird();
}
//验证接口
Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
return pet;
} //实验
var jim = new OnePetShop();
var dog = jim.sellPetshop("dog");
dog.run(); var tom = new TwoPetShop();
var pig = tom.sellPetshop("pig");
pig.run();
} // factory3(); function factory4(){
var Pet = new Interface("Pet",["eat","run","sing","register"]);
//宠物基类
function BasePet(){
this.register = function(){
document.write("宠物登记 。。。<br>")
}
this.eat = function(){
document.write("吃顿饱饭 <br>")
}
}
//实现
function Dog(){
Dog.superClass.constructor.call(this);
this.run = function(){
document.write("小狗跑步 。。<br>")
}
this.sing = function(){
document.write("小狗唱歌。。。<br>")
}
}
function Cat(){
Cat.superClass.constructor.call(this);
this.run = function(){
document.write("小猫跑步 。。<br>")
}
this.sing = function(){
document.write("小猫唱歌。。。<br>")
}
}
function Pig(){
Pig.superClass.constructor.call(this);
this.run = function(){
document.write("小猪跑步 。。<br>")
}
this.sing = function(){
document.write("小猪唱歌。。。<br>")
}
}
function Bird(){
Bird.superClass.constructor.call(this);
this.run = function(){
document.write("小鸟跑步 。。<br>")
}
this.sing = function(){
document.write("小鸟唱歌。。。<br>")
}
} //继承
extend(Dog,BasePet);
extend(Pig,BasePet);
extend(Bird,BasePet);
extend(Cat,BasePet); //智能工厂
var PetFactory = {
sellPetShop:function(kind){
var pet;
pet= eval("new "+kind +"()");
Interface.ensureImplements(pet,Pet);//在工厂中验证接口关系。
return pet;
}
} //1.把核心的商店变成一个抽象类
var Petshop = function(){}
Petshop.prototype = {
sellPetShop:function(kind){
var pet = this.sellPetshop(kind);
pet.eat();
pet.register();
return pet;
},
sellPetshop:function(model){
throw new Error("this is abstract class")
}
}
//2.利用子类满足上边的需求(多态)
var OnePetShop = function(){}
extend(OnePetShop,Petshop);
OnePetShop.prototype.sellPetshop = function(model){
var pet=null;
var pets = ["Dog","Cat","Bird"];
for( v in pets){
if(pets[v] == model){
pet = PetFactory.sellPetShop(model);
// Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
break;
}
}
return pet;
} var TwoPetShop = function(){}
extend(TwoPetShop,Petshop);
TwoPetShop.prototype.sellPetshop = function(model){
var pet=null;
var pets = ["Pig"];
for( v in pets){
if(pets[v] == model){
pet = PetFactory.sellPetShop(model);
// Interface.ensureImplements(pet,Pet);
pet.eat();
pet.register();
break;
}
}
return pet;
} //测试
var twoPetShop = new TwoPetShop();
twoPetShop.sellPetshop("Pig"); var jim = new OnePetShop();
jim.sellPetshop("Dog");
// jim.sellPetShop("Pig");
}
factory4();
</script>
</body>
</html>
JavaScript设计模式-9.工厂模式的更多相关文章
- JavaScript设计模式之工厂模式
一.工厂模式概念 工厂模式定义一个用于创建对象的接口,这个接口由子类决定实例化哪一个类.该模式使一个类的实例化延迟到了子类.而子类可以重写接口方法以便创建的时候指定自己的对象类型(抽象工厂). 这个模 ...
- JavaScript设计模式(3)-工厂模式
工厂模式 1. 简单工厂 简单工厂:使用一个类或对象封装实例化操作 假如我们有个自行车商店类 BicycleShop,它提供了销售自行车的方法可以选择销售两类自行车 Speedster,Comfort ...
- JavaScript设计模式--简单工厂模式
一,介绍 工厂模式创建对象(视为工厂里的产品)时无需指定创建对象的具体类. 工厂模式定义一个用于创建对象的接口,这个接口由子类决定实例化哪一个类.该模式使一个类的实例化延迟到了子类.而子类可以重写接口 ...
- javaScript设计模式之----工厂模式
什么是工厂模式?我们通过一个例子了解一下: 比如我们想要弹出几个字符串 function funA(){ alert('a'); } function funB(){ alert('b'); } fu ...
- JavaScript设计模式-10.工厂模式实例xhr
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【javascript】javascript设计模式之工厂模式
1.要解决的问题 2.如何实现 3.与构造函数的区别 4.总结 1.要解决的问题 工厂模式通常用于重复创建相似对象,提供动态创建对象的接口. 2.工厂模式最为设计模式中构造模式之一,通常在类或类的静态 ...
- javascript设计模式-抽象工厂模式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript设计模式--简单工厂模式例子---XHR工厂
第一步,Ajax操作接口(目的是起一个接口检测作用) (1)引入接口文件 //定义一个静态方法来实现接口与实现类的直接检验 //静态方法不要写出Interface.prototype ,因为这是写到接 ...
- JavaScript 设计模式之工厂模式
随机推荐
- ubuntu 安装 zend studio
hi,everyone!2014 年到了,是20你死还是爱你一世,世人不得而知.夜观天象,道德依旧在沦丧,经济依然在滑坡.行了,就整这几句.最近在折腾linux,这篇文章,没有什么意义.只是找找写bl ...
- 有關更新Java 至UPDATE 45 後出現沒法進入ORACLE EBS
近日部份使用者在更新Java 至UPDATE 45 後出現沒法進入ORACLE. 解決方法如下. 在開始 => 程式集 => JAVA => Configure Java中 (Ja ...
- TSQL--如何突破PRINT的8000大限
相信很多DBA都喜欢干的一件事就是拼SQL语句,我也不例外,但是PRINT只能打印4000的Unicode string或8000的Non-unicode string, 这个蛋疼的限制会导致过长的s ...
- .net 开发者尝试Apache Spark™
本文编译自一篇msdn magazine的文章,原文标题和链接为: Test Run - Introduction to Spark for .NET Developers https://msdn. ...
- zookeeper的主要应用
master选举 数据发布和订阅 负载均衡
- 查看jar包的jdk版本并降级
用解压工具打开jar包(例子都是用7zip) 进入到META-INF目录,查看MANIFEST.MF文件,查看Bulid-Jdk,下图就为1.7.0_55版本的JDK,这就表示jetty-serv ...
- Oracle数据泵导出导入(expdp/impdp)
一.创建表空间 create tablespace atp logging datafile 'D:\oracle\oradata\orcl\atp.dbf' size 50m autoextend ...
- docker-compose 部署 Redis
信息: Docker版本($ docker --version):Docker版本18.06.1-ce,版本e68fc7a 系统信息($ cat /etc/centos-release):CentOS ...
- Programmatically Disable Event Firing on List Item Update in SharePoint 2010
1. Microsoft.SharePoint.dll Create EventFiring.cs 1.Right-click on the project, select Add and click ...
- Remoteland HDU - 4196
题意: 给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数 解析: 最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均 ...