// Folder var Folder = function (name) { this.name = name; this.parent = null; this.files = []; };
Folder.prototype.add = function (file) { file.parent = this; this.files.push(file); };
Folder.prototype.remove = function () { if (!this.parent) {
return;
}
for (var files = this.parent.files, i = 0; i < files.length; i++) {
var file = files[i];
if (file === this) {
files.splice(i, 1);
break;
}
} };
Folder.prototype.scan = function () { console.log('开始扫描文件夹:' + this.name); for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
file.scan();
} };
// File var File = function (name) { this.name = name; this.parent = null; };
File.prototype.add = function () { throw new Error('文件夹下面不能在添加文件'); };
Folder.prototype.remove = function () { if (!this.parent) {
return;
}
for (var files = this.parent.files, i = 0; i < files.length; i++) {
var file = files[i];
if (file === this) {
files.splice(i, 1);
break;
}
} };
File.prototype.scan = function () { console.log('开始扫描文件:' + this.name); };
// 测试 var folder = new Folder('学习资料'); var folder1 = new Folder('JavaScript'); folder1.add(new File('JavaScript设计模式与开发实践')); folder.add(folder1); folder.add(new File('深入浅出Node.js'));
把水煮沸 用沸水冲泡咖啡 把咖啡倒进杯子 加糖和牛奶 var Coffee = function () { }; Coffee.prototype.boilWater = function () { console.log('把水煮沸'); }; Coffee.prototype.brewCoffeeGriends = function () { console.log('用沸水冲泡咖啡'); }; Coffee.prototype.pourInCup = function () { console.log('把咖啡倒进杯子'); }; Coffee.prototype.addSugarAndMilk = function () { console.log('加糖和牛奶'); }; Coffee.prototype.init = function () { this.boilWater(); this.brewCoffeeGriends(); this.pourInCup(); this.addSugarAndMilk(); }; var coffee = new Coffee(); coffee.init(); 泡一壶茶
把水煮沸 用沸水浸泡茶叶 把茶水倒进杯子 加柠檬 var Tea = function () { }; Tea.prototype.boilWater = function () { console.log('把水煮沸'); }; Tea.prototype.steepTeaBag = function () { console.log('用沸水浸泡茶叶'); }; Tea.prototype.pourInCup = function () { console.log('把茶水倒进杯子'); }; Tea.prototype.addLemon = function () { console.log('加柠檬'); }; Tea.prototype.init = function () { this.boilWater(); this.steepTeaBag(); this.pourInCup(); this.addLemon(); }; var tea = new Tea(); tea.init(); 分离出共同点
把水煮沸 用沸水冲泡饮料 把饮料倒进杯子 加调料 var Beverage = function () { }; Beverage.prototype.boilWater = function () { console.log('把水煮沸'); }; Beverage.prototype.brew = function () { }; // 空方法,应该由子类重写 Beverage.prototype.pourInCup = function () { }; // 空方法,应该由子类重写 Beverage.prototype.addCondiments = function () { }; // 空方法,应该由子类重写 Beverage.prototype.init = function () { this.boilWater(); this.brew(); this.pourInCup(); this.addCondiments(); }; 创建Coffee子类
var Coffee = function () { }; Coffee.prototype = new Beverage(); Coffee.prototype.brew = function () { console.log('用沸水冲泡咖啡'); }; Coffee.prototype.pourInCup = function () { console.log('把咖啡倒进杯子'); }; Coffee.prototype.addCondiments = function () { console.log('加糖和牛奶'); }; var coffee = new Coffee(); coffee.init(); 创建Tea子类
var Tea = function () { }; Tea.prototype = new Beverage(); Tea.prototype.brew = function () { console.log('用沸水浸泡茶叶'); }; Tea.prototype.pourInCup = function () { console.log('把茶倒进杯子'); }; Tea.prototype.addCondiments = function () { console.log('加柠檬'); }; var tea = new Tea(); tea.init(); Beverage.prototype.init 被称为模板方法的原因是,该方法中封装了子类的算法框架,它作 为一个算法的模板,指导子类以何种顺序去执行哪些方法。在 Beverage.prototype.init 方法中, 算法内的每一个步骤都清楚地展示在我们眼前。
// 原始的飞机类 var Plane = function () { } Plane.prototype.fire = function () { console.log('发射普通子弹'); };
// 接下来增加两个装饰类,分别是导弹和原子弹: var MissileDecorator = function (plane) { this.plane = plane; }; MissileDecorator.prototype.fire = function () { this.plane.fire(); console.log('发射导弹'); }; var AtomDecorator = function (plane) { this.plane = plane; }; AtomDecorator.prototype.fire = function () { this.plane.fire(); console.log('发射原子弹'); };
// 运行 var plane = new Plane(); plane = new MissileDecorator(plane); plane = new AtomDecorator(plane); plane.fire(); // 分别输出: 发射普通子弹、发射导弹、发射原子弹 JavaScript 装饰者模式
var plane = { fire: function () {
console.log('发射普通子弹');
} };
var missileDecorator = function () { console.log('发射导弹'); };
var atomDecorator = function () { console.log('发射原子弹'); };
var fire1 = plane.fire; plane.fire = function () { fire1(); missileDecorator(); };
var fire2 = plane.fire; plane.fire = function () { fire2(); atomDecorator(); };
plane.fire(); // 分别输出: 发射普通子弹、发射导弹、发射原子弹
状态模式 状态模式的关键是区分事物内部的状态,事物内部状态的改变往往会带来事物的行为改变。
例子:电灯的 弱光、强光、关灯 切换。
// OffLightState: var OffLightState = function (light) { this.light = light; }; OffLightState.prototype.buttonWasPressed = function () { console.log('弱光'); // offLightState 对应的行为 this.light.setState(this.light.weakLightState); // 切换状态到 weakLightState };
// WeakLightState: var WeakLightState = function (light) { this.light = light; }; WeakLightState.prototype.buttonWasPressed = function () { console.log('强光'); // weakLightState 对应的行为 this.light.setState(this.light.strongLightState); // 切换状态到 strongLightState };
// StrongLightState: var StrongLightState = function (light) { this.light = light; }; StrongLightState.prototype.buttonWasPressed = function () { console.log('关灯'); // strongLightState 对应的行为 this.light.setState(this.light.offLightState); // 切换状态到 offLightState };
var Light = function () { this.offLightState = new OffLightState(this); this.weakLightState = new WeakLightState(this); this.strongLightState = new StrongLightState(this); this.button = null; };
Light.prototype.init = function () { var button = document.createElement('button'),
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。