JS "eval智能" 工厂模式
<script>
var Shop = function () {
this.name = function () { document.write("商店的名字 <br/>"); },
this.local = function () { document.write("商店的位置 <br/>"); }
}; var ToolShop = function () {
ToolShop.convertASThis.constructor.call(this);
this.kind = function () { document.write("卖工具的商店 <br/>"); },
this.time = function () { document.write("工具商店营业时间8:00~16:00 <br/>"); }
}; var PetShop = function () {
PetShop.convertASThis.constructor.call(this);
this.kind = function () { document.write("卖猫狗的商店 <br/>"); },
this.time = function () { document.write("营业时间9:00~15:00 <br/>"); }
}; extend(ToolShop, Shop);
extend(PetShop, Shop); //========================================================= var Fish_ToolShop = function () {
Fish_ToolShop.convertASThis.constructor.call(this);
this.material = function () { document.write("钓鱼所用的材料! <br/>"); };
} var Repair_ToolShop = function () {
Repair_ToolShop.convertASThis.constructor.call(this);
this.material = function () { document.write("修理所用的材料! <br/>"); };
} extend(Repair_ToolShop, ToolShop);
extend(Fish_ToolShop, ToolShop); //========================================================= //智能工厂,动态实例化商店
var ShopFactory = {
Init: function (shopName) {
var tmpObj = eval("new " + shopName + "();");
return tmpObj;
}
}; //========================================================= //不同的工具商店可能存在多种不同类型的工具,所以存在一个商店的父类但是不能被实例化,需要在子类中重写
var FatherToolShop = function () {
this.sellTool = function (kind) {
throw new Error('this is a abstract class');
};
}; //这个对象继承FatherToolShop后重写sellTool来来进行实例
var OtherToolShop = function () { };
extend(OtherToolShop, FatherToolShop); //继承之后将原型链中的sellTool方法重写
OtherToolShop.prototype.sellTool = function (kind) {
var kind = kind;
var kinds = ["Repair_ToolShop", "Fish_ToolShop"]; for (var v in kinds) {
if (kinds[v] == kind) {
kind = ShopFactory.Init(kind);
kind.kind(); //工具商店
kind.time(); //营业时间
break;
}
}
return kind;
}; //========================================================= var test = new OtherToolShop();
test.sellTool("Repair_ToolShop").material();
test.sellTool("Fish_ToolShop").material();
</script>
JS "eval智能" 工厂模式的更多相关文章
- JS设计模式--简单工厂模式
在JS中创建对象会习惯的使用new关键字和类构造函数(也是可以用对象字面量). 工厂模式就是一种有助于消除两个类依赖性的模式. 工厂模式分为简单工厂模式和复杂工厂模式,这篇主要讲简单工厂模式. 简单工 ...
- [JS设计模式]:工厂模式(3)
简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口. 这种模式主要用在所实例化的类型在编译期并不能确定, 而是在执行期决定的情况. 说的通俗点,就像公司茶水间的饮料 ...
- JS设计模式之工厂模式
1 什么是工厂模式? 工厂模式是用来创建对象的一种最常用的设计模式.我们不暴露创建对象的具体逻辑,而是将将逻辑封装在一个函数中,那么这个函数就可以被视为一个工厂.工厂模式根据抽象程度的不同可以分为: ...
- JS面向对象之工厂模式
js面向对象 什么是对象 "无序属性的集合,其属性可以包括基本值.对象或者函数",对象是一组没有特定顺序的的值.对象的没个属性或方法都有一个俄名字,每个名字都映射到一个值. 简单来 ...
- js设计模式:工厂模式、构造函数模式、原型模式、混合模式
一.js面向对象程序 var o1 = new Object(); o1.name = "宾宾"; o1.sex = "男"; o1.a ...
- js简单的工厂模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- JS中的工厂模式
.一个栗子: var BicycleShop = function(){}; BicycleShop.prototype = { sellBicycle : function( model ){ va ...
- JS设计模式——7.工厂模式(示例-XHR)
XHR工厂 基本实现 var AjaxHandler = new Interface('AjaxHandler', ['request', 'createXHR']); var SimpleHandl ...
- JS设计模式——7.工厂模式(概念)
工厂模式 本章讨论两种工厂模式: 简单工厂模式 使用一个类(通常是一个单体)来生成实例. 使用场景:假设你想开几个自行车商店(创建自行车实例,组装它,清洗它,出售它),每个店都有几种型号的自行车出售. ...
随机推荐
- hadoop的Map阶段的四大步骤
深入理解map的几个阶段是怎样执行的.
- python卸载或者安装时提示There is a problem with this Windows Installer package.A program required for this install to complete could not be run. Contact your support personnel or package vendor
1.卸载时报这个错,先进行下修复,再执行卸载: 2.安装时报这个错,安装的过程中,没有取得管理员的权限. Msi格式的文件,点右键后,也没有“以管理员身份运行”的菜单项,那怎么办呢?你可以点“开始”菜 ...
- iOS开发-UIImageView响应点击事件
UIImageView是不能够响应点击事件的,在开发过程中我们需要经常对头像等添加点击事件,上网搜索一番后发现有如下两个方法: 1.找到点击图片Event,添加事件处理函数 UIImageView.u ...
- transformClassesWithJarMergingForDebug
Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.> com.android.build ...
- C语言中打印返回值
demo: ----return :返回值------------------ int mosquitto_username_pw_set(struct mosquitto *mosq, const ...
- CMS4.0——后知后觉
前言: 2016年底,自己作为参与者加入CMS3.0的改版中:2017年中,CMS4.0在经过一个月有余的时间,华丽丽的蜕变成现在大家喜闻乐见的:http://news.gangguwang.com/ ...
- mysql分组查询获取组内某字段最大的记录
id sid cid 1 1 12 1 23 2 1 以sid分组,最后取cid最大的那一条,以上要取第2.3条 1 方法一: select * from (select * from table o ...
- Android——简单对话框实现
点击一个Button,弹出一个简单的对话框: bn3.setOnClickListener(new View.OnClickListener() { public void onClick(View ...
- PowerDesigner 同名问题解决 Entity Attribute name uniqueness
选择"Tools -> Model Options"后 "Allow reuse"复选框,建议把这个钩也去掉 Tool->check model.. ...
- 使用 mysql workbench 建议
在日常使用mysql workbench时,未免操作失误,不建议启用远程管理.