学习JavaScript设计模式(六)
来源:互联网 发布:初中考试软件 编辑:程序博客网 时间:2024/06/01 09:49
Flyweight(享元)模式
Flyweight 模式是一种经典的结构型解决方案,主要时用于优化重复、缓慢及数据共享效率低下的代码。
Flweight 旨在通过与相关对象共享尽可能多的数据来减少应用程序中内存的使用。
应用方式:
- 用于数据层,处理内存中保存的大量相似对象的共享数据。
- 用于DOM层,Flyweight 作为中央事件处理器,来避免将事件处理程序附加到父容器中的每个元素,而是将事件处理程序附加到这个父容器上。
两个状态:
- 内部状态:独立于具体的场景,通常不会改变,可以被一些对象共享
- 外部状态:取决于具体的场景,并根据场景而变化,外部状态不能被共享
实现步骤:
- 将所有外在数据从目标类剥离。具体做法是尽可能多地删除该类的属性,所删除的应该是那种因实例而异的属性。构造参数也要这样处理。这些参数应该被添加到该类的各个方法。
- 创建一个用来控制该类的实例化的工厂。用一个对象字面量保存每一个这类对象的引用,并以用来生成这些对象的参数的唯一性组合作为它们的索引。另一种方法是对象池技术。
- 创建一个用来保存外在数据的管理器。外在数据被保存在管理器内的一个数据结构中。管理器随后会根据需要将这些数据提供给共享对象的方法,其效果就如同该类有许多实例一样。
简单实现:
/** * Created by Zang on 2017/3/21. *//** * 书籍管理 * 书籍基本属性:id title author price * 书籍状态:availability(可用) checkoutDate(借出日期) checkoutMember(借出人) dueReturnDate(归还时间) */// 没有优化之前function Book(id, title, author, checkoutDate, checkoutMember, dueReturnDate, availability) { this.id = id; this.title = title; this.author = author; this.checkoutDate = checkoutDate; this.checkoutMember = checkoutMember; this.dueReturnDate = dueReturnDate; this.availability = availability;}Book.prototype = { getID: function () { return this.id; }, getTitle: function () { return this.title; }, getAuthor: function () { return this.author; }, // 更新书籍的状态 updateCheckoutStatus: function (checkoutDate, checkoutMember, dueReturnDate, availability) { this.checkoutDate = checkoutDate; this.checkoutMember = checkoutMember; this.dueReturnDate = dueReturnDate; this.availability = availability; }, // 续借书籍 extendCheckoutPeriod: function (dueReturnDate) { this.dueReturnDate = dueReturnDate; }, // 是否到期 isPastDue: function (id) { var currentDate = new Date(); return currentDate.getTime() > Date.parse(this.dueReturnDate); }};/********************************************************************/// 优化目标函数,剥离外部数据function Book(id, title, author) { this.id = id; this.title = title; this.author = author;}Book.prototype = { getID: function () { return this.id; }, getTitle: function () { return this.title; }, getAuthor: function () { return this.author; }};// 控制该类的实例化的工厂var BookFactory = (function () { var existingBooks = {}; return { createBook: function (id, title, author) { /*查找之前是否创建*/ var existingBook = existingBooks[id]; if (existingBook) { return existingBook; } else { /* 如果没有,就创建一个,然后保存*/ var book = new Book(id, title, author); existingBooks[id] = book; return book; } } }})();// 数据的管理器var BookRecordManager = (function () { var bookRecordDatabase = {}; return { /*添加借书记录*/ addBookRecord: function (id, title, author, checkoutDate, checkoutMember, dueReturnDate, availability) { var book = BookFactory.createBook(id, title, author); bookRecordDatabase[id] = { checkoutMember: checkoutMember, checkoutDate: checkoutDate, dueReturnDate: dueReturnDate, availability: availability, book: book }; }, // 更新书籍的状态 updateCheckoutStatus: function (id, checkoutDate, checkoutMember, dueReturnDate, availability) { bookRecordDatabase[id].availability = availability; bookRecordDatabase[id].checkoutDate = checkoutDate; bookRecordDatabase[id].checkoutMember = checkoutMember; bookRecordDatabase[id].dueReturnDate = dueReturnDate; }, // 续借书籍 extendCheckoutPeriod: function (id, dueReturnDate) { bookRecordDatabase[id].dueReturnDate = dueReturnDate; }, // 是否到期 isPastDue: function (id) { var currentDate = new Date(); return currentDate.getTime() > Date.parse(bookRecordDatabase[id].dueReturnDate); } };})();
缺点:这种模式在优化代码的同时,也提高了其复杂程度,这会给调试和维护造成困难。
0 0
- 学习JavaScript设计模式(六)
- 设计模式学习(六)
- 设计模式学习(六)创建者模式
- javascript设计模式介绍(六)寄生构造函数模式
- 设计模式(六)
- 设计模式学习笔记六(Prototype原型模式)
- 设计模式学习系列六:组合模式(composite)
- php设计模式学习系列(六)--建造者模式
- 设计模式学习笔记(六)建造者模式
- javascript 设计模式学习
- 设计模式学习(六):模版模式
- JavaScript学习(六)
- JavaScript学习(六)
- 学习JavaScript设计模式(一)
- 学习JavaScript设计模式(二)
- 学习JavaScript设计模式(三)
- 学习JavaScript设计模式(四)
- 学习JavaScript设计模式(五)
- Unity3D_uGUI学习笔记(5)_Animation Integration简述
- 大数据相关资源
- kafka跟storm收集日志解决方案
- swap file “*.swp”already exists
- MJPEG协议入门介绍
- 学习JavaScript设计模式(六)
- linux 端口被占用解决
- C++篇----开篇(1)
- java基础_day0022_java关键字_this_super_构造器_子类对象实例化
- nswoj G
- Hadoop2.X中Mapreduce,YARN等配置,启动和常见错误解决办法
- for循环,定时器,闭包混合一块的那点事。
- 用JavaScript生成验证码
- js 获取复选框被选中的值 jquery