JS實(shí)現(xiàn)簡(jiǎn)單的圖書館享元模式實(shí)例
更新時(shí)間:2015年06月30日 12:13:44 作者:方方和圓圓
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)單的圖書館享元模式,以一個(gè)圖書館存書借書的例子分析了圖書館享元模式的實(shí)現(xiàn)技巧,需要的朋友可以參考下
本文實(shí)例講述了JS實(shí)現(xiàn)簡(jiǎ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>
希望本文所述對(duì)大家的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)文章
JS實(shí)現(xiàn)簡(jiǎn)單的頂部定時(shí)關(guān)閉層效果
這篇文章主要介紹了通過JS實(shí)現(xiàn)的簡(jiǎn)單頂部定時(shí)關(guān)閉層效果,需要的朋友可以參考下2014-06-06
利用JS測(cè)試目標(biāo)網(wǎng)站的打開響應(yīng)速度
本文簡(jiǎn)單說明利用JS來測(cè)試目標(biāo)網(wǎng)站的打開響應(yīng)速度,方法簡(jiǎn)單明了大家一看就明白并附上了腳本源碼2017-12-12
yolov5項(xiàng)目部署+微信小程序前端展示的全過程
YOLOV5模型從發(fā)布到現(xiàn)在都是炙手可熱的目標(biāo)檢測(cè)模型,被廣泛運(yùn)用于各大場(chǎng)景之中,下面這篇文章主要給大家介紹了關(guān)于yolov5項(xiàng)目部署+微信小程序前端展示的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
JavaScript對(duì)象訪問器Getter及Setter原理解析
這篇文章主要介紹了JavaScript對(duì)象訪問器Getter及Setter原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
實(shí)現(xiàn)變速回到頂部的JavaScript代碼
一般網(wǎng)頁(yè)的下方都會(huì)放置一個(gè)置頂按鈕, 尤其是頁(yè)面底部沒有導(dǎo)航的網(wǎng)頁(yè), 這樣可以幫助訪客重新找到導(dǎo)航或者重溫一遍廣告 (想得真美).2011-05-05
Sample script that displays all of the users in a given SQL
Sample script that displays all of the users in a given SQL Server DB...2007-06-06

