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 设计模式之工厂模式
随机推荐
- Vivado 2017封装自定义IP Core
使用Vivado2017.3自定义IP Core.通常情况下,我们做设计采用模块化设计,对于已经设计好的一部分模块功能,就可以直接拿来调用,IP Core就是这样来的,一般来说我们看不到IP Core ...
- zabbix监控cpu jumps
cpu监控图形分为三种 cpu jumps cpu突发 包含 context switches per second 进程线程切换 interrupts per second 每秒的中断次数 cpu ...
- java提示找不到或无法加载主类
背景 默许jdk的配置大家都没有问题,执行java,javac无报错,但今天在尝试在本地起来kafka的时候,提示java 找不到或无法加载主类,然后日志中提示 Files 找不到或无法加载主类:C: ...
- JQuery对象函数
1.JQuery对象函数写法格式 简单示例: main代表对象名,main.roleName = (function () { return $('#RoleName').val();})();表示为 ...
- 批判“await使用中的阻塞和并发”——对asyc/await这对基友的误会和更正
写第一篇<await使用中的阻塞和并发>的时候还自信满满,觉得写的真不错,结果漏洞百出…… 更正第二篇<await使用中的阻塞和并发(二)>的时候觉得这回不会再错了…… 结果我 ...
- Docker 网络模式和跨主机通信
Docker的四种网络模式 Bridge模式 当Docker进程启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上.虚拟网桥的工作方式和物理交 ...
- 201621123023《Java程序设计》第9周学习总结
一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 二.书面作业 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至少2种在List中删除 ...
- Gogland使用 - 非常简单查看Go语言源代码全貌!
Go语言也支持面向对象开发,不过和以往我们所使用的面向对象开发还是有不同,Go语言主张组合方式形成类的概念,在Go语言中,结构起到很大作用,如果用结构组合字段和方法,那么单纯在源代码中看,真的是费时费 ...
- 20165219 Exp1 PC平台逆向破解
20165219 Exp1 PC平台逆向破解 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串 ...
- [Swift实际操作]七、常见概念-(14)使用UIColor设置界面组件的颜色属性
打开移动应用程序,不可避免的需要和颜色打交道.本文将为你演示颜色对象的使用. 首先导入需要使用到的界面工具框架 import UIKit 通过UIColor的属性,可以获得橙色.右侧的实时反馈区,显示 ...