JavaScript设计模式之模板方法模式原理与用法示例
作者:咕咚萝卜 发布时间:2024-02-24 02:17:20
标签:JavaScript,设计模式,模板方法模式
本文实例讲述了JavaScript设计模式之模板方法模式原理与用法。分享给大家供大家参考,具体如下:
一、模板方法模式:一种只需使用继承就可以实现的非常简单的模式。
二、模板方法模式由两部分组成,第一部分是抽象父类,第二部分是具体的实现子类。
三、以设计模式中的Coffee or Tea来说明模板方法模式:
1、模板Brverage,代码如下:
var Beverage = function(){};
Beverage.prototype.boilWater = function(){
console.log('把水煮沸');
};
Beverage.prototype.pourInCup = function(){
throw new Error( '子类必须重写pourInCup' );
};
Beverage.prototype.addCondiments = function(){
throw new Error( '子类必须重写addCondiments方法' );
};
Beverage.prototype.customerWantsConditions = function(){
return true; //默认需要调料
};
Beverage.prototype.init = function(){
this.boilWater();
this.brew();
this.pourInCup();
if(this.customerWantsCondiments()){
//如果挂钩返回true,则需要调料
this.addCondiments();
}
};
2、子类继承父类
var CoffeeWithHook = function(){};
CoffeeWithHook.prototype = new Beverage();
CoffeeWithHook.prototype.brew = function(){
console.log('把咖啡倒进杯子');
};
CoffeeWithHook.prototype.addCondiments = function(){
console.log('加糖和牛奶');
};
CoffeeWithHook.prototype.customerWantsCondiments = function(){
return window.confirm( '请问需要调料吗?' );
};
3、煮一杯咖啡
var coffeeWithHook = new CoffeeWithHook();
coffeeWithHook.init();
四、另一种写法
var Beverage = function( param ){
var boilWater = function(){
console.log( '把水煮沸' );
};
var brew = param.brew || function(){
throw new Error( '必须传递brew方法' );
};
var pourInCup = param.pourInCup || function(){
throw new Error( '必须传递pourInCup方法' );
};
var addCondiments = param.addCondiments || function(){
throw new Error( '必须传递addCondiments方法' );
};
var F = function(){};
F.prototype.init = function(){
boilWater();
brew();
pourInCup();
addCondiments();
};
return F;
};
var Coffee = Beverage({
brew: function(){
console.log( '用沸水冲泡咖啡' );
},
pourInCup: function(){
console.log('把咖啡倒进杯子');
},
addCondiments: function(){
console.log('加糖和牛奶');
}
});
var coffee = new Coffee();
coffee.init();
测试运行结果:
希望本文所述对大家JavaScript程序设计有所帮助。
来源:https://blog.csdn.net/joyksk/article/details/79853344
0
投稿
猜你喜欢
- 问题背景两张表一张是用户表a(主键是int类型),一张是用户具体信息表b(用户表id字段是varchar类型)。因为要显示用户及用户信息,所
- 在数据预处理过程中可能需要将列的顺序颠倒,有两种方法。import numpy as npimport pandas as pddf = p
- 如何通过PHP实现Des加密算法代码实例注:php7以上不支持了,因为php7去掉了某些函数, 另外变量的{}要改为[]<?phpcl
- PIL(Python Imaging Library)是Python中一个强大的图像处理库,但目前其只支持到Python2.7pillow是
- Python内建的filter()函数用于过滤序列。和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,fil
- 为什么要使用滤波消除图像中的噪声成分叫作图像的平滑化或滤波操作。信号或图像的能量大部分集中在幅度谱的低频和中频段是很常见的,而在较高频段,感
- 一般来说,pytorch 的Parameter是一个tensor,但是跟通常意义上的tensor有些不一样1) 通常意义上的tensor 仅
- 一、命名元祖在python基础中, 我们学习元祖的时候,取元祖内部的元素都是通过索引来进行取值的。但是这种取值方式不够友好, 所以我们引入命
- privot多对多关系的中间表。PT5框架会自动把privot带上。我们需要隐藏,因为我们不需要privot,而且pritvot也不在我们模
- 项目结构:源代码:# -*- coding: utf-8 -*-"""@date: 2022/01
- python读取.txt(.log)文件 、.xml 文件 、excel文件数据,并将数据类型转换为需要的类型,添加到list中详解1.读取
- 今天发现一个使用python写的管理cisco设备的小框架tratto,可以用来批量执行命令。下载后主要有3个文件:Systems.py 定
- 去除HTML代码中所有标签<% '****************************** '函数:RemoveH
- 摘要:不同方法读取excel中的多个不同sheet表格性能比较# 方法1def read_excel(path): df=pd.
- 这里的Counter是指collections中的Counter,通过Counter可以实现字典的创建以及字典key出现频次的统计。然而,使
- python 遍历字符串(含汉字)实例详解s = "中国china"for j in s: print j首
- 一 开发环境集成开发工具:jupyter notebook 6.2.5集成开发环境:python 3.10.6第三方库:nump
- cuda上tensor的定义a = torch.ones(1000,1000,3).cuda()某一gpu上定义cuda1 = torch.
- 一组有序项目的集合可变的数据类型【可进行增删改查】列表中可以包含任何数据类型,也可包含另一个列表【可任意组合嵌套】列表是以方括号“[]”包围
- 载入库绘制表格我们需要用到python库中的matplotlib库import matplotlib.pyplot as plt一、折线图#