Created
March 30, 2014 14:00
-
-
Save lmartins/9873184 to your computer and use it in GitHub Desktop.
Decorator Pattern with CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book | |
constructor: (@author, @title, @numCopies) -> | |
display: -> | |
console.log """Book | |
\t Author #{@author} | |
\t Title #{@title} | |
\t Copies #{@numCopies} | |
""" | |
class Video | |
constructor: (@director, @title, @numCopies, @playTime) -> | |
display: -> | |
console.log """Video | |
\t Director: #{@director} | |
\t Title: #{@title} | |
\t Copies: #{@numCopies} | |
\t Play Time: #{@playTime} | |
""" | |
class Decorator | |
constructor: (@libraryItem) -> | |
display: -> | |
@libraryItem.display() | |
class Borrowable extends Decorator | |
constructor: (@libraryItem) -> | |
borrowers: [] | |
addBorrower: (borrower) -> | |
@borrowers.push borrower | |
removeBorrower: (borrower) -> | |
@borrowers[t..t] = [] if ( t = @borrowers.indexOf(borrower) ) > -1 | |
borrowItem: (name) -> | |
@addBorrower name | |
@libraryItem.numCopies-- | |
returnItem: (name) -> | |
@removeBorrower name | |
@libraryItem.numCopies++ | |
display: -> | |
super() | |
console.log "Borrowers: \n\t #{borrower for borrower in @borrowers}" | |
book = new Book("Dostoevsky", "Crime and punishment", 5) | |
book.display() | |
video = new Video("JJ Abrams", "Star Trek", 17, 126) | |
video.display() | |
console.log "Making video borrowable:" | |
borrowVideo = new Borrowable(video) | |
borrowVideo.borrowItem "Customer #1" | |
borrowVideo.borrowItem "Customer #2" | |
borrowVideo.display() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment