Skip to content

Commit 11ff0a6

Browse files
committed
Add Proxy Design Patterns
1 parent 12c6035 commit 11ff0a6

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

Proxy/1/PublicLibrary.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var PublicLibrary = function(books)
2+
{
3+
// implements Library
4+
this.catalog =
5+
{
6+
};
7+
for(var i = 0, len = books.length; i < len; i++)
8+
{
9+
this.catalog[books[i].getIsbn()] =
10+
{
11+
book : books[i],
12+
available : true
13+
};
14+
}
15+
};
16+
PublicLibrary.prototype =
17+
{
18+
findBooks : function(searchString)
19+
{
20+
var results = [];
21+
for(var isbn in this.catalog)
22+
{
23+
if(!this.catalog.hasOwnProperty(isbn))
24+
continue;
25+
if(searchString.match(this.catalog[isbn].getTitle()) || searchString.match(this.catalog[isbn].getAuthor()))
26+
{
27+
results.push(this.catalog[isbn]);
28+
}
29+
}
30+
return results;
31+
},
32+
checkoutBook : function(book)
33+
{
34+
var isbn = book.getIsbn();
35+
if(this.catalog[isbn])
36+
{
37+
if(this.catalog[isbn].available)
38+
{
39+
this.catalog[isbn].available = false;
40+
return this.catalog[isbn];
41+
}
42+
else
43+
{
44+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' is not currently available.');
45+
}
46+
}
47+
else
48+
{
49+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
50+
}
51+
},
52+
returnBook : function(book)
53+
{
54+
var isbn = book.getIsbn();
55+
if(this.catalog[isbn])
56+
{
57+
this.catalog[isbn].available = true;
58+
}
59+
else
60+
{
61+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
62+
}
63+
}
64+
};

Proxy/1/PublicLibraryProxy.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var PublicLibraryProxy = function(catalog)
2+
{
3+
// implements Library
4+
this.library = new PublicLibrary(catalog);
5+
};
6+
PublicLibraryProxy.prototype =
7+
{
8+
findBooks : function(searchString)
9+
{
10+
return this.library.findBooks(searchString);
11+
},
12+
checkoutBook : function(book)
13+
{
14+
return this.library.checkoutBook(book);
15+
},
16+
returnBook : function(book)
17+
{
18+
return this.library.returnBook(book);
19+
}
20+
};

Proxy/Virtual/PublicLibrary.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var PublicLibrary = function(books)
2+
{
3+
// implements Library
4+
this.catalog =
5+
{
6+
};
7+
for(var i = 0, len = books.length; i < len; i++)
8+
{
9+
this.catalog[books[i].getIsbn()] =
10+
{
11+
book : books[i],
12+
available : true
13+
};
14+
}
15+
};
16+
PublicLibrary.prototype =
17+
{
18+
findBooks : function(searchString)
19+
{
20+
var results = [];
21+
for(var isbn in this.catalog)
22+
{
23+
if(!this.catalog.hasOwnProperty(isbn))
24+
continue;
25+
if(searchString.match(this.catalog[isbn].getTitle()) || searchString.match(this.catalog[isbn].getAuthor()))
26+
{
27+
results.push(this.catalog[isbn]);
28+
}
29+
}
30+
return results;
31+
},
32+
checkoutBook : function(book)
33+
{
34+
var isbn = book.getIsbn();
35+
if(this.catalog[isbn])
36+
{
37+
if(this.catalog[isbn].available)
38+
{
39+
this.catalog[isbn].available = false;
40+
return this.catalog[isbn];
41+
}
42+
else
43+
{
44+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' is not currently available.');
45+
}
46+
}
47+
else
48+
{
49+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
50+
}
51+
},
52+
returnBook : function(book)
53+
{
54+
var isbn = book.getIsbn();
55+
if(this.catalog[isbn])
56+
{
57+
this.catalog[isbn].available = true;
58+
}
59+
else
60+
{
61+
throw new Error('PublicLibrary: book ' + book.getTitle() + ' not found.');
62+
}
63+
}
64+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var PublicLibraryVirtualProxy = function(catalog)
2+
{
3+
// implements Library
4+
this.library = null;
5+
this.catalog = catalog;
6+
// Store the argument to the constructor.
7+
};
8+
PublicLibraryVirtualProxy.prototype =
9+
{
10+
_initializeLibrary : function()
11+
{
12+
if(this.library === null)
13+
{
14+
this.library = new PublicLibrary(this.catalog);
15+
}
16+
},
17+
findBooks : function(searchString)
18+
{
19+
this._initializeLibrary();
20+
return this.library.findBooks(searchString);
21+
},
22+
checkoutBook : function(book)
23+
{
24+
this._initializeLibrary();
25+
return this.library.checkoutBook(book);
26+
},
27+
returnBook : function(book)
28+
{
29+
this._initializeLibrary();
30+
return this.library.returnBook(book);
31+
}
32+
};

0 commit comments

Comments
 (0)