JS實(shí)現(xiàn)簡單的圖書館享元模式實(shí)例
更新時(shí)間:2015年06月30日 12:13:44 作者:方方和圓圓
這篇文章主要介紹了JS實(shí)現(xiàn)簡單的圖書館享元模式,以一個(gè)圖書館存書借書的例子分析了圖書館享元模式的實(shí)現(xiàn)技巧,需要的朋友可以參考下
本文實(shí)例講述了JS實(shí)現(xiàn)簡單的圖書館享元模式。分享給大家供大家參考。具體如下:
<!DOCTYPE html> <html> <head> <title>享員模式</title> </head> <body> <script> /* *flyweight 享員模式 */ //例子是一個(gè)圖書館存書借書 ->_-> var Book = function(id, title, author, genre, pageCount, publisherId, ISBN, checkoutDate, checkoutMember /*還有一些*/){ this.id = id; this.title = title; this.author = author; this.genre = this.genre; this.pageCount = pageCount; this.publisherId = publisherId; this.ISBN = ISBN; /*...*/ this.checkoutDate = checkoutDate; this.checkoutMember = checkoutMember; }; Book.prototype = { getTitle : function(){ return this.title; }, getAuthor : function(){ return this.author; }, getISBN : function(){ return this.ISBN; }, /*__more.._*/ updateCheckoutStatus : function(booId,checkoutDate,checkoutMember){ this.id = bookId; this.checkoutDate = checkoutDate; this.checkoutMember = checkoutMember; /*_more.._*/ } }; //下面介紹享元的版本;PS(使用了一個(gè)OBJ存書籍,這樣就可以存多的書) var BookFactory = (function(){ var existingBooks = {},existingBook; return { createBook : function(title,author,genre,ISBN){ existingBook = existingBooks[ISBN]; if(existingBook){ return existingBook; }else{ var book = new Book(/*_moreData_bookInfo == _*/) return existingBooks[ISBN] = book; } } } })(); var BookRecordManager = (function(){ var bookRecordDatabase = {}; return { addBookRecord : function(id,ISNB/* == */){ var book = BookFactory.createBook(/**/); bookRecordDatabase[id] = { checkoutDate : checkoutDate, checkoutMember : checkoutMember }; }, updateCheckoutStatus : function(bookId,xx){ bookRecordDatabase[bookId] = { xx : tt, oo : yy } }, extend : function(){ /*自定義各種公用方法了*/ } } })(); </script> </body> </html>
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- javascript設(shè)計(jì)模式之享元模式
- javascript設(shè)計(jì)模式 – 享元模式原理與用法實(shí)例分析
- javascript 設(shè)計(jì)模式之享元模式原理與應(yīng)用詳解
- JavaScript享元模式原理與用法實(shí)例詳解
- JavaScript設(shè)計(jì)模式之享元模式實(shí)例詳解
- JavaScript使用享元模式實(shí)現(xiàn)文件上傳優(yōu)化操作示例
- js設(shè)計(jì)模式之結(jié)構(gòu)型享元模式詳解
- 輕松掌握J(rèn)avaScript享元模式
- 學(xué)習(xí)JavaScript設(shè)計(jì)模式之享元模式
- JavaScript設(shè)計(jì)模式之性能優(yōu)化模式享元模式
相關(guān)文章
Express代理轉(zhuǎn)發(fā)服務(wù)器實(shí)現(xiàn)
這篇文章主要為大家介紹了Express代理轉(zhuǎn)發(fā)服務(wù)器實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05解決layui動(dòng)態(tài)加載復(fù)選框無法選中的問題
今天小編就為大家分享一篇解決layui動(dòng)態(tài)加載復(fù)選框無法選中的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript運(yùn)動(dòng)框架 多物體任意值運(yùn)動(dòng)(三)
這篇文章主要為大家詳細(xì)介紹了JavaScript運(yùn)動(dòng)框架的第三部分,多物體任意值運(yùn)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05JavaScript實(shí)現(xiàn)圖片偽異步上傳過程解析
這篇文章主要介紹了javascript實(shí)現(xiàn)圖片偽異步上傳過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04