>  기사  >  웹 프론트엔드  >  간단한 라이브러리 플라이웨이트 모드 example_javascript 기술의 JS 구현

간단한 라이브러리 플라이웨이트 모드 example_javascript 기술의 JS 구현

WBOY
WBOY원래의
2016-05-16 15:52:151088검색

이 기사의 예에서는 JS에서 구현된 간단한 라이브러리 플라이웨이트 모드를 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부 내용은 다음과 같습니다.

<!DOCTYPE html>
<html>
<head>
<title>享员模式</title>
</head>
<body>
<script>
 /*
  *flyweight 享员模式
  */
 //例子是一个图书馆存书借书 ->_->
 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(使用了一个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 프로그래밍 설계에 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.