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 设计模式之工厂模式
随机推荐
- 23 DesignPatterns学习笔记:C++语言实现 --- 2.2 Adapter
23 DesignPatterns学习笔记:C++语言实现 --- 2.2 Adapter 2016-07-22 (www.cnblogs.com/icmzn) 模式理解
- 深水划水队项目---七天冲刺之day5
站立式会议: 因为今天有成员回家,不能进行线下站立式会议,只能线上进行语音聊天 工作进度: 昨天完成的任务: 游戏功能的基本实现 商讨出如何实现游戏中的难度选择功能与道具功能 商讨出站立式会议能线下 ...
- Java动态代理(三)——模拟AOP实现
以下案例模拟AOP实现 目录结构 接口PersonService package com.ljq.service; public interface PersonService { public vo ...
- Java反射API研究(4)——Class中的重要对象
一.Constructor与Method的父类:Executable Executable表示一个可执行类,构造方法与普通方法都是Executable AnnotatedType[] getAnnot ...
- IPv4&&IPv6地址结构分析
IPv4套接字地址结构: 套接字都需要有一个指向套接字地址结构的指针作为参数.每个协议簇都定义它自己的套接字地址结构.这些结构的名字均已sockaddr_开头,并以对应每个协议族的唯一后缀结尾. wi ...
- EAS系统环境的搭建
(一)应用服务器配置 1.先建立好程序需要部署的文件夹,如D:\AppServer\,此目录下包含如下几个子目录: XClient(客户端升级程序放置的目录,此目录下应包含Config和Files子目 ...
- LRUCache c#
LRUCache是Least Recently Used 近期最少使用算法的缓存,是android提供的一个缓存工具类.可以以两种排序方式来输出缓存,一种是按插入顺序输出,一种是按最近最少方式输出,最 ...
- RoadFlow ASP.NET Core工作流配置文件说明
工作流配置文件及说明如下: { "Logging": { "LogLevel": { "Default": "Warning&qu ...
- iOS错误 - too many open files (error = 24)
碰到这个错误是在用 UIImageView 显示图片的时候.UIImage 用的是 imageNamed 方法.错误原因是打开了太多的文件.应该是太多文件的打开导致了 UIImage 的 cache ...
- Mybatis 优化:
Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...