17.03 - publiclibrary class with chain of responsibility catalogs.js

来自「JS设计模式源代码」· JavaScript 代码 · 共 48 行

JS
48
字号
/* PublicLibrary class, with genre catalogs in a chain of responsibility. */var PublicLibrary = function(books, firstGenreCatalog) { // implements Library  this.catalog = {};  this.firstGenreCatalog = firstGenreCatalog;  for(var i = 0, len = books.length; i < len; i++) {    this.addBook(books[i]);  }};PublicLibrary.prototype = {  findBooks: function(searchString) { ... },  checkoutBook: function(book) { ... },  returnBook: function(book) { ... },  addBook: function(newBook) {    // Always add the book to the main catalog.    this.catalog[newBook.getIsbn()] = { book: newBook, available: true };    // Try to add the book to each genre catalog.    this.firstGenreCatalog.handleFilingRequest(newBook);  }};// -----------------------------------------------------------------------------// Usage example.// ----------------------------------------------------------------------------- // Instantiate the catalogs.var biographyCatalog = new BiographyCatalog();var fantasyCatalog = new FantasyCatalog();var mysteryCatalog = new MysteryCatalog();var nonFictionCatalog = new NonFictionCatalog();var sciFiCatalog = new SciFiCatalog();// Set the links in the chain.biographyCatalog.setSuccessor(fantasyCatalog);fantasyCatalog.setSuccessor(mysteryCatalog);mysteryCatalog.setSuccessor(nonFictionCatalog);nonFictionCatalog.setSuccessor(sciFiCatalog);// Give the first link in the chain as an argument to the constructor.var myLibrary = new PublicLibrary(books, biographyCatalog);// You can add links to the chain whenever you like.var historyCatalog = new HistoryCatalog();sciFiCatalog.setSuccessor(historyCatalog);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?