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.工厂模式(概念)
工厂模式 本章讨论两种工厂模式: 简单工厂模式 使用一个类(通常是一个单体)来生成实例. 使用场景:假设你想开几个自行车商店(创建自行车实例,组装它,清洗它,出售它),每个店都有几种型号的自行车出售. ...
随机推荐
- 使用 vux 框架
1)vux官网:https://vux.li/#/ 2)通过 vue-cli 工具使用 vux 1.如果没有安装 nodejs,请先前往 nodejs 官网下载并安装 nodejs,传送门:https ...
- iOS开发-VFL初窥
VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...
- Nginx 访问日志
配置访问日志: [root@localhost ~]$ cat /usr/local/nginx/conf/nginx.conf http { log_format main '$remote_add ...
- sutdio中替换全局方法
Ctril Shift R Text to find :是要搜索的内容或者要被替换的内容 Replace with :是要替换的内容 Preview:是可以看到预览 在Scope的选项卡里 Whole ...
- WP8.1学习系列(第十九章)——事件和路由事件概述
我们将介绍在使用 C#.Visual Basic 或 Visual C++ 组件扩展 (C++/CX) 作为编程语言并使用 XAML 进行 UI 定义时,针对 Windows 运行时应用的事件的编程概 ...
- [原]rpm安装rpm-package报错:Header signature NOKEY 和 error: Failed dependencies:
以前经常遇到这个问题,一直未有记录,今天记录下来: 在安装rpm包的时候报错误如下: Question 1: warning: *.rpm: Header V3 DSA signature: NOKE ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Redmine插件的安装与卸载,知识库插件安装。
本文介绍linux版本的Redmine插件安装,通常Redmine安装在Linux系统,/var/www/redmine/路径. 安装: 复制插件到 2.X版本 #{RAILS_ROOT}/plugi ...
- JavaScript—当前时间
当前时间-倒计时下载 效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content ...
- Unity3D笔记 愤怒的小鸟<六> 弹弓发射小鸟
要实现的目标 实现个性化的鼠标 实现弹弓 选择小鸟.拉升弹弓.发射小鸟 弹弓橡皮筋 声音 1.实现个性化鼠标 效果 2.添加弹弓 建立两个材质 创建一个空GameObject 把两个shoot拖进来统 ...