当前位置: 首页 > news >正文

JavaScript 设计模式之享元模式

享元

将一部分共用的方法提取出来作为公用的模块

const Car = {getName: function () {return this.name},getPrice: function (price) {return price * 30}
}const BMW = function (name, price) {this.name = namethis.price = price
}
BMW.prototype = Car
const bmw = new BMW('BMW', 1000000)
console.log(bmw.getName()) // BMW
console.log(bmw.getPrice(1000000)) // 3000000const Benz = function (name, price) {this.name = namethis.price = price
}
Benz.prototype = Car
const benz = new Benz('Benz', 2000000)
console.log(benz.getName()) // Benz
console.log(benz.getPrice(2000000)) // 6000000

享元模式的应用目的是为了提高程序的执行效率与系统的性能。因此在大型系统开发中应用是比较广泛的,有时可以发生质的改变。它可以避免程序中的数据重复。有时系统内存在大量对象,会造成大量存占用,所以应用享元模式来减少内存消耗是很有必要的。

模板方法

假使我们有如下的样式

.panel {width: 200px;min-height: 50px;box-shadow: 0 0 10px rgba(0, 0, 0, .5);padding: 10px;margin: auto
}.btn-content {display: flex;justify-content: space-around;
}
.btn-content.right{flex-direction: row-reverse;
}

创建一个弹窗的基类


const Alert = function (data) {if (!data) returnthis.content = data.contentthis.panel = document.createElement('div')this.contentNode = document.createElement('p')this.confirmBtn = document.createElement('span')this.closeBtn = document.createElement('b')this.footerBtn = document.createElement('div')this.footerBtn.className = 'btn-content'this.panel.className = 'panel'this.confirmBtn.className = 'btn-confirm'this.closeBtn.className = 'btn-close'this.confirmBtn.innerHTML = data.confirm || '确认'this.closeBtn.innerHTML = data.close || '关闭'this.contentNode.innerHTML = data.content || ''this.success = data.success || function () { }this.cancel = data.cancel || function () { }
}Alert.prototype = {init: function () {this.panel.appendChild(this.contentNode)this.footerBtn.appendChild(this.confirmBtn)this.footerBtn.appendChild(this.closeBtn)this.panel.appendChild(this.footerBtn)document.body.appendChild(this.panel)this.bindEvent()this.show()},bindEvent: function () {this.confirmBtn.onclick = () => {this.success()this.hide()}this.closeBtn.onclick = () => {this.cancel()this.hide()}},show: function () {this.panel.style.display = 'block'},hide: function () {this.panel.style.display = 'none'}
}

基类主要用来实现一些常规的样式布局

定义一个标准的提示框


const TitleAlert = function (data) {Alert.call(this, data)this.title = data.titlethis.titleDom = document.createElement('h3')this.titleDom.style.textAlign = 'center'this.titleDom.innerHTML = this.titlethis.panel.className += ' title-panel'
}
TitleAlert.prototype = new Alert
TitleAlert.prototype.init = function () {this.panel.insertBefore(this.titleDom, this.panel.firstChild)Alert.prototype.init.call(this)
}

确认按钮位置在左/右


const LeftAlert = function (data) {TitleAlert.call(this, data)this.panel.className += ' left-panel'this.footerBtn.className += ' left'
}
LeftAlert.prototype = new Alert
LeftAlert.prototype.init = function () {TitleAlert.prototype.init.call(this)
}const RightAlert = function (data) {TitleAlert.call(this, data)this.panel.className += ' right-panel'this.footerBtn.className += ' right'
}
RightAlert.prototype = new Alert
RightAlert.prototype.init = function () {TitleAlert.prototype.init.call(this)
}

使用

new LeftAlert({title: '提示',content: '这是一个自定义的右上角弹窗',btnText: '确定',success: function () {console.log('点击了确定按钮');},cancel: function () {console.log('点击了取消按钮');}
}).init();

效果

模板方法的核心在于对方法的重用,它将核心方法封装在基类中,让子类继承基类的方法,实现基类方法的共享,达到方法共用。

http://www.lryc.cn/news/303154.html

相关文章:

  • 利用故事推动企业变革:如何提升数据分析技能
  • Python内置函数04——enumerate
  • unity学习(28)——登录功能
  • Mac公证脚本-Web公证方式
  • 让你专注工作的思维模板,进入每天的专注生活
  • Java之获取Nginx代理之后的客户端IP
  • 【springboot+vue项目(十五)】基于Oauth2的SSO单点登录(二)vue-element-admin框架改造整合Oauth2.0
  • 音频的传输链路与延迟优化点
  • 【51单片机】直流电机驱动(PWM)(江科大)
  • 腾讯文档(excel也一样)设置单元格的自动行高列宽
  • vue-router 提供的几种导航守卫
  • Element UI 组件的安装及使用
  • 网站架构演变、LNP+Mariadb数据库分离、Web服务器集群、Keepalived高可用
  • 设计模式(七):策略模式(行为型模式)
  • 人工智能|深度学习——基于对抗网络的室内定位系统
  • MySQL的配置文件my.cnf正常的配置项目
  • 小程序API能力集成指南——界面导航栏API汇总
  • onlyoffice基础环境搭建+部署+demo可直接运行 最简单的入门
  • ubuntu 22.04 图文安装
  • Dockerfile文件中只指定挂载点会发生什么?
  • 详解 leetcode_078. 合并K个升序链表.小顶堆实现
  • OpenHarmony下gn相关使用
  • 怎样重置ubuntu mysql8密码
  • SpringBoot+WebSocket实现即时通讯(三)
  • vue3前端项目开发,具备纯天然的防止爬虫采集的特征
  • js 多对象去重(多属性去重)
  • 在 JavaScript 中,Map 与 object 的差别?为什么有 object 还需要 Map?
  • 【研究生复试】计算机软件工程人工智能研究生复试——资料整理(速记版)——自我介绍(英文)
  • ACP科普:IDEAL含义及应用
  • 【GO语言卵细胞级别教程】06.GO语言的字符串操作