学习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
原创粉丝点击