03.09.2015 Views

Design Patterns

Download - Assembla

Download - Assembla

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

184<br />

CHAPTER 13 ■ THE FLYWEIGHT PATTERN<br />

function isLeapYear(y) {<br />

return (y > 0) && !(y % 4) && ((y % 100) || !(y % 400));<br />

}<br />

this.months = [];<br />

// The number of days in each month.<br />

this.numDays = [31, isLeapYear(this.year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30,<br />

31, 30, 31];<br />

for(var i = 0, len = 12; i < len; i++) {<br />

this.months[i] = new CalendarMonth(i, this.numDays[i], this.element);<br />

}<br />

);<br />

CalendarYear.prototype = {<br />

display: function() {<br />

for(var i = 0, len = this.months.length; i < len; i++) {<br />

this.months[i].display(); // Pass the call down to the next level.<br />

}<br />

this.element.style.display = 'block';<br />

}<br />

};<br />

/* CalendarMonth class, a composite. */<br />

var CalendarMonth = function(monthNum, numDays, parent) { // implements CalendarItem<br />

this.monthNum = monthNum;<br />

this.element = document.createElement('div');<br />

this.element.style.display = 'none';<br />

parent.appendChild(this.element);<br />

this.days = [];<br />

for(var i = 0, len = numDays; i < len; i++) {<br />

this.days[i] = new CalendarDay(i, this.element);<br />

}<br />

);<br />

CalendarMonth.prototype = {<br />

display: function() {<br />

for(var i = 0, len = this.days.length; i < len; i++) {<br />

this.days[i].display(); // Pass the call down to the next level.<br />

}<br />

this.element.style.display = 'block';<br />

}<br />

};<br />

/* CalendarDay class, a leaf. */<br />

var CalendarDay = function(date, parent) { // implements CalendarItem<br />

this.date = date;<br />

this.element = document.createElement('div');<br />

this.element.style.display = 'none';

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!