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 设计模式之工厂模式
随机推荐
- 基于httpcore(httpclient component)搭建轻量级http服务器
下面是apache官网例子 服务器端接受请求,实现接收文件请求处理器 import java.io.File; import java.io.IOException; import java.io.I ...
- asp.net部署时加密config文件
1:运行cmd,并定位到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(可以直接运行vs2005的命令提示工具,但是貌似vs2010默认指向的framewo ...
- Python作图笔记
感谢莫烦大神,附带他的个人网站链接:https://morvanzhou.github.io/ 再带上官方的文档,多看文档啊!不然参数忘了就没地方查了:https://matplotlib.org/a ...
- python 中为什么不需要重载
函数重载主要是为了解决两个问题. (1)可变参数类型. (2) 可变参数个数. 另外,一个基本的设计原则是,仅仅当两个函数除了参数类型和参数个数不同以外,其功能是完全相同的,此时才使用函数重载,如果两 ...
- [Erlang25]Erlang in anger 翻译
Erlang in anger Erlang in anger 是写Learn some Erlang的帅小伙(照片真是帅死啦)写的,一共87页,可以随意下载(英文原版):http://www ...
- ie11下ajax用escape发送中文参数失败
一个项目中,登录请求是ajax,get模式.登录名无中文可以正常登录:登录名是中文则偶尔可以登录,大部分情况下无法登录,ajax请求无法发送成功. 登录名是用js的escape函数转码. 经过多次测试 ...
- File Path Directory总结
阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo 对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...
- 加固apk的开发者最常面对的十种问题
欢迎访问网易云社区,了解更多网易技术产品运营经验. 因为工信部对移动App应用安全过检要求日益增多,不加固大都达不到工信部的要求,同时开发者加固App大都是为了防止以下10个检测项出现问题,影响App ...
- 201621123023《Java程序设计》第14周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 由于我的系 ...
- 【文文殿下】洛谷P2408 不同子串个数
题目链接https://www.luogu.org/problemnew/show/P2408 SAM裸题,大力求就行了 #include<cstdio> #include<cstr ...