模板方法模式笔记
   父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤
   实例:弹出框归一化插件

css样式

             .alert{position: fixed;left: 50%;top: 50%;background-color: #ffffff;z-index:;width:400px;
border-radius: 5px;font-weight: bold;color: #535e66;box-shadow: 1px 1px 4px #eee,-1px -1px 4px #eee;margin-top:-9rem;margin-left:-200px;}
.alert h3{text-indent:2rem;border-bottom:1px solid #eef0f1;margin:;padding:1rem 0;}
.alert p{padding: 2rem 5rem;height: 5rem;overflow: hidden;margin:;border-bottom:1px solid #eef0f1;}
.alert .a-close{display: block;cursor: pointer;width: 12px;height: 12px;position: absolute;top: 1.2rem;right: 1.5rem;background: url(../img/icons.png) -48px -96px no-repeat;}
.alert .a-close:after{display: block;content: ""; clear: both;}
.alert .btn{display: inline-block;cursor: pointer;width: 4.5rem;height: 1.8rem;line-height: 1.8rem;text-align: center;color: #FFFFFF;
border-radius: 5px;margin:0.5rem;}
.alert .panelFooter{width:11rem;float:right;}
.alert .a-confirm{background-color: #0095d9;}
.alert .cancel{background-color: #546a79;}
.alert .right{float:right} @media only screen and (min-width: 100px) and (max-width: 640px) {
.alert{width:80%;margin-left:-40%;}
.alert h3{text-indent: 1rem;}
.alert p{padding:1rem;}
.alert .a-close{right: 1rem;}
}

运用寄生组合继承方法

             //原型式继承
function inheritobject(o){
//声明一个过渡函数对象
function F(){
}
//过渡原型对象继承父对象
F.prototype=o;
//返回过渡对象的一个实列,该实例的原型继承了父对象
return new F();
} /*
*寄生式继承 继承原型
* 传递参数subclass 子类
* 传递参数superclass 父类
* */
function inheritPrototype(subclass,superclass){
//复制一份父类的原型副本保存在变量中
var p=inheritobject(superclass.prototype);
//修正因为重写子类原型导致子类的constructor属性被修改
p.constructor=subclass;
//设置子类原型
subclass.prototype=p;
}

首先要创建基本提示框基类(模板类),然后其他提示框类只需要在继承的基础上,拓展自己所需即可,日后需求变更,修改基础类,所有提示框类实现的样式都会统一变化

基本提示框类:只实现一个提示内容、一个关闭按钮、一个确定按钮

            var Alert = function(data){
if(!data) return;
this.content=data.content;
this.panel =document.createElement("div");//提示框面板
this.contentNode=document.createElement("p");
this.panelFooter=document.createElement("div");
this.confirmBtn=document.createElement("span");
this.closeBtn=document.createElement("b");
this.panel.className="alert";
this.panelFooter.className="panelFooter";
this.confirmBtn.className="btn a-confirm";
this.closeBtn.className="a-close";
this.confirmBtn.innerHTML=data.confirm || '确认';
this.contentNode.innerHTML=this.content;
this.success=data.success || function(){};
this.fail=data.fail || function(){};
}
Alert.prototype={
init :function(){
this.panel.appendChild(this.closeBtn);
this.panel.appendChild(this.contentNode);
this.panelFooter.appendChild(this.confirmBtn);
this.panel.appendChild(this.panelFooter);
document.body.appendChild(this.panel);
this.bindEvent();
this.show();
},
bindEvent : function(){
var me = this;
this.closeBtn.onclick = function(){
me.fail();
me.hide();
}
this.confirmBtn.onclick = function(){
me.success();
me.hide();
}
},
hide : function(){
this.panel.style.display ="none";
},
show :function(){
this.panel.style.display='block';
}
}

右侧按钮提示框类:继承基本提示框类,拓展需求,使按钮移动到右侧显示

             //右侧按钮提示框
var RightAlert =function(data){
Alert.call(this,data);
this.panelFooter.className = this.panelFooter.className + ' right';
}
inheritPrototype(RightAlert,Alert);

标题提示框类 :继承基本提示框类,拓展需求,添加一个提示框标题

            //标题提示框类
var TitleAlert=function(data){
Alert.call(this,data);
this.title = data.title;
this.titleNode = document.createElement("h3");
this.titleNode.innerHTML = this.title;
}
inheritPrototype(TitleAlert,Alert);
TitleAlert.prototype.init =function(){
this.panel.insertBefore(this.titleNode,this.panel.firstChild);
Alert.prototype.init.call(this); }

继承类也可作为模板

带有取消按钮的弹出框:继承标题提示框类,拓展需求,添加一个取消按钮

             //继承类也可作为模板
var CancelAlert = function(data){
//继承标题提示框构造函数
TitleAlert.call(this,data);
//取消按钮文案
this.cancel = data.cancel;
this.cancelBtn = document.createElement('span');
this.cancelBtn.className='btn cancel';
this.cancelBtn.innerHTML = this.cancel || '取消';
}
inheritPrototype(CancelAlert,TitleAlert);
CancelAlert.prototype.init = function(){
TitleAlert.prototype.init.call(this);
this.panelFooter.appendChild(this.cancelBtn);
}
CancelAlert.prototype.bindEvent = function(){
var me=this;
TitleAlert.prototype.bindEvent.call(me);
this.cancelBtn.onclick = function(){
me.fail();
me.hide();
}
}

测试代码

           new CancelAlert({
title : '提示',
content : '提示内容',
success : function(){
console.log('ok');
},
fail : function(){
console.log("cancel");
}
}).init();

浏览器显示

javascript-模板方法模式-提示框归一化插件的更多相关文章

  1. javascript模板方法模式

    一:什么是模板方法模式: 模板方法模式由二部分组成,第一部分是抽象父类,第二部分是具体实现的子类,一般的情况下是抽象父类封装了子类的算法框架,包括实现一些公共方法及封装子类中所有方法的执行顺序,子类可 ...

  2. 轻松掌握:JavaScript模板方法模式

    模板方法模式 假如我们有一些对象,各个对象之间有一些相同的行为,也有一些不同的行为,这时,我们就可以用模板方法模式来把相同的部分上移到它们的共同原型中(父类),而将不同的部分留给自己各自重新实现. 模 ...

  3. Jquery 弹出提示框输入插件 apprise 修改中文按钮以及使用说明

      apprise的使用非常简单,引入js脚本和css <script type="text/javascript" src="/js/apprise-1.5.fu ...

  4. jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)

    http://www.51xuediannao.com/js/jquery/jquery_tsk/ http://www.jq22.com/demo/jqueryConfirm20160413/

  5. javascript中简单提示框

    CSS部分 .help-tip{ width: 340px; border:1px solid #A0A0A0; background-color: #F8F8F8; border-radius: 5 ...

  6. JavaScript实现延时提示框

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  7. 基于jquery的提示框JavaScript 插件,类Bootstrap

    目录 基于jquery的提示框JavaScript 插件,类Bootstrap 基于jquery的提示框JavaScript 插件,类Bootstrap 源码 github地址: https://gi ...

  8. 再起航,我的学习笔记之JavaScript设计模式17(模板方法模式)

    模板方法模式 由模板方法模式开始我们正式告别结构型设计模式,开始行为型设计模式的学习分享 行为型设计模式用于不同对象之间职责划分或算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模 ...

  9. javascript设计模式(张容铭)学习笔记 - 照猫画虎-模板方法模式

    模板方法模式(Template Method):父类中定义一组操作算法骨架,而降一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤. 项目经理体验了各个页面的 ...

随机推荐

  1. windows10简单试用(多图,连薛定谔的猫都杀死了)

    为了大家看起来方便,我的截图都是gif的,比较小,但是颜色会有色差,相信大家不介意的 昨天windows10可以下载第一时间就下了玩玩 由于是技术预览,所以不打算替换之前的系统,只装在虚拟机里玩玩就好 ...

  2. 跟我一起hadoop(1)-hadoop2.6安装与使用

    伪分布式 hadoop的三种安装方式: Local (Standalone) Mode Pseudo-Distributed Mode Fully-Distributed Mode 安装之前需要 $ ...

  3. ABP源码分析十三:缓存Cache实现

    ABP中有两种cache的实现方式:MemroyCache 和 RedisCache. 如下图,两者都继承至ICache接口(准确说是CacheBase抽象类).ABP核心模块封装了MemroyCac ...

  4. Oracle---------sql 中取值两列中值最大的一列

    1.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列. select (case when a>b then a else b ...

  5. JS对象继承篇

    JS对象继承篇 ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现的 原型链 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法 function Person() ...

  6. 三、Redis基本操作——List

    小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...

  7. .NET Core的文件系统[2]:FileProvider是个什么东西?

    在<读取并监控文件的变化>中,我们通过三个简单的实例演示从编程的角度对文件系统做了初步的体验,接下来我们继续从设计的角度来继续认识它.这个抽象的文件系统以目录的形式来组织文件,我们可以利用 ...

  8. HTTPS那些事(一)HTTPS原理

    转载来自:http://www.guokr.com/post/114121/ 谣言粉碎机前些日子发布的<用公共WiFi上网会危害银行账户安全吗?>,文中介绍了在使用HTTPS进行网络加密传 ...

  9. Node.js 给前端带来了什么

    在软件开发领域,前端工程师曾经是一个比较纠结的职业.在Web技术真正发展起来之前的相当长一段时间里,由于技术门槛很低,前端工程师行业一直是鱼龙混杂的状态.其中很多号称是Web开发者的人实际上并没有什么 ...

  10. 绑定一个值给radio

    在ASP.NET MVC程序中,需要给一个radio list表绑定一个值. 下面是Insus.NET实现的方法: 使用foreach来循环radio每一个选项,如果值与选项的值相同,那这个选项为选中 ...