Proxy Design Pattern
Structural: Proxy Pattern
Could you please explain the proxy design pattern?
View Answer:
Interview Response: The Proxy Pattern, as the name suggests, functions as a proxy or placeholder for another object to manage access, decrease cost, and simplify the code. The proxy could connect to almost anything – a network connection, a large object in memory, a file, or another resource that would have been expensive or impossible to recreate.
Code Example #1:


This pattern's objects are as follows:
Client -- Example code: the run() function
- To request an operation, call proxy.
Proxy -- Example code: GeoProxy
- provides a user interface similar to the real object
- and maintains a reference to the real object that allows the proxy to access it.
- Responds to requests and forwards them to the actual object.
RealSubject -- Example code: GeoCoder
- specifies the actual object for which service is getting requested
function Flyweight(make, model, processor) {
this.make = make;
this.model = model;
this.processor = processor;
}
let FlyWeightFactory = (function () {
let flyweights = {};
return {
get: function (make, model, processor) {
if (!flyweights[make + model]) {
flyweights[make + model] = new Flyweight(make, model, processor);
}
return flyweights[make + model];
},
getCount: function () {
let count = 0;
for (let f in flyweights) count++;
return count;
},
};
})();
function ComputerCollection() {
let computers = {};
let count = 0;
return {
add: function (make, model, processor, memory, tag) {
computers[tag] = new Computer(make, model, processor, memory, tag);
count++;
},
get: function (tag) {
return computers[tag];
},
getCount: function () {
return count;
},
};
}
let Computer = function (make, model, processor, memory, tag) {
this.flyweight = FlyWeightFactory.get(make, model, processor);
this.memory = memory;
this.tag = tag;
this.getMake = function () {
return this.flyweight.make;
};
// ...
};
function run() {
let computers = new ComputerCollection();
computers.add('Dell', 'Studio XPS', 'Intel', '5G', 'Y755P');
computers.add('Dell', 'Studio XPS', 'Intel', '6G', 'X997T');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', 'U8U80');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', 'NT777');
computers.add('Dell', 'Studio XPS', 'Intel', '2G', '0J88A');
computers.add('HP', 'Envy', 'Intel', '4G', 'CNU883701');
computers.add('HP', 'Envy', 'Intel', '2G', 'TXU003283');
console.log('Computers: ' + computers.getCount());
console.log('Flyweights: ' + FlyWeightFactory.getCount());
}
run();
/*
OUTPUT:
Computers: 7
Flyweights: 2
*/
Code Example #2:


/* External API*/
var FlightListAPI = function () {
//creation
};
FlightListAPI.prototype = {
getFlight: function () {
// get master list of flights
console.log('Generating flight List');
},
searchFlight: function (flightDetails) {
// search through the flight list based on criteria
console.log('Searching for flight');
},
addFlight: function (flightData) {
// add a new flight to the database
console.log('Adding new flight to DB');
},
};
// creating the proxy
var FlightListProxy = function () {
// getting a reference to the original object
this.flightList = new FlightListAPI();
};
FlightListProxy.prototype = {
getFlight: function () {
return this.flightList.getFlight();
},
searchFlight: function (flightDetails) {
return this.flightList.searchFlight(flightDetails);
},
addFlight: function (flightData) {
return this.flightList.addFlight(flightData);
},
};
console.log('----------With Proxy----------');
const proxy = new FlightListProxy();
console.log(proxy.getFlight());
/*
OUTPUT
----------With Proxy----------
Generating flight List
*/
Code Example #3:
class GetCapital {
getMycapital(country) {
if (country === 'Pakistan') {
return 'Islamabad';
} else if (country === 'India') {
return 'New Delhi';
} else if (country === 'Canada') {
return 'Ottawa';
} else if (country === 'Egypt') {
return 'Cairo';
} else {
return '';
}
}
}
class ProxyGetCapital {
constructor() {
this.capital = new GetCapital();
this.cache = {};
}
getMycapital(country) {
if (!this.cache[country]) {
var value = this.capital.getMycapital(country);
this.cache[country] = value;
return `${value}--Returning From GetCaptial`;
} else {
return `${this.cache[country]}--Returning from Cache`;
}
}
}
var capital = new ProxyGetCapital();
console.log(capital.getMycapital('Pakistan'));
console.log(capital.getMycapital('India'));
console.log(capital.getMycapital('Canada'));
console.log(capital.getMycapital('Egypt'));
console.log(capital.getMycapital('Egypt'));
console.log(capital.getMycapital('Egypt'));
console.log(capital.getMycapital('Pakistan'));
console.log(capital.getMycapital('Pakistan'));
console.log(capital.getMycapital('Canada'));
/*
OUTPUT:
Islamabad--Returning From GetCaptial
New Delhi--Returning From GetCaptial
Ottawa--Returning From GetCaptial
Cairo--Returning From GetCaptial
Cairo--Returning from Cache
Cairo--Returning from Cache
Islamabad--Returning from Cache
Islamabad--Returning from Cache
Ottawa--Returning from Cache
*/
The Proxy pattern belongs to which pattern family?
View Answer:
Interview Response: The Proxy pattern is a type of structural design pattern.
What is a good use case for the Proxy Pattern?
View Answer:
Interview Response: The proxy pattern attempts to lighten the load on the target object. It is useful when dealing with large applications that make many network requests. Because delays may occur when responding to such requests, using a proxy pattern prevents the target object from becoming overburdened with requests.
HTTP requests are a real-world example. Because these are costly operations, the proxy pattern aids in reducing the number of requests forwarded to the target.
HTTP requests are a real-world example. Because these are costly operations, the proxy pattern aids in reducing the number of requests forwarded to the target.
What are some of the advantages of employing the Proxy pattern?
View Answer:
Interview Response: Benefits of the Proxy Pattern.
- You have control over the service object without the client being aware of it.
- You can control the lifecycle of the service object even if your clients do not care.
- The proxy functions even if the service object is not ready or unavailable.
- Open/Closed Principle. Users can add new proxies without interrupting the service or clients.
What are some of the disadvantages of employing the Proxy pattern?
View Answer:
Interview Response: Drawbacks of the Proxy Pattern.
- Because you'll be introducing many new classes, the code may become more complicated.
- The service's response time may be delayed or hindered.