Search⌘ K

Challenge: Solution Review

Explore the proxy design pattern by reviewing a JavaScript solution that restricts students' access to certain apps and websites through a library kiosk interface. Understand how the proxy controls access, blocks restricted resources, and optimizes requests. This lesson solidifies your grasp of structural patterns for coding interviews.

We'll cover the following...

Solution #

Node.js
class LibraryKiosk{
open(app){
console.log(`Opening ${app}`)
}
connectTo(website)
{
console.log("Connecting to "+ website);
}
}
class ProxyLibraryKiosk{
constructor(){
this.libraryKiosk = new LibraryKiosk()
this.blockedSites = ["fb.com", "instagram.com","snapchat.com","google.com", "gmail.com"]
this.blockedApps = ["camera", "photos", "music", "settings"]
}
open(app){
if(this.blockedApps.includes(app)){
console.log(`You can't access the ${app}`)
}else{
this.libraryKiosk.open(app)
}
}
connectTo(website){
if(this.blockedSites.includes(website)){
console.log(`Access to ${website} denied`)
}else{
this.libraryKiosk.connectTo(website)
}
}
}
var libraryKiosk = new ProxyLibraryKiosk();
libraryKiosk.open("photos")
libraryKiosk.open("Chrome")
libraryKiosk.connectTo("booksportal.com");
libraryKiosk.connectTo("google.com");

Explanation

In this challenge, you had to implement the proxy pattern, such that it restricted the use of the kiosk to access apps/websites for the students. As mentioned in the problem statement, a student can access/connect to any app/site using the LibraryKiosk class.

class LibraryKiosk{
    open(app){
  
...