Intermediate LevelSoftware Engineering Principles
What is the Proxy Pattern and how does it control access to underlying objects?
❓ Interview Prompt: Explain the Proxy Structural Pattern, Virtual Proxy (lazy loading), Protection Proxy (access control), and Caching Proxy implementations.
The Proxy Pattern provides a surrogate or placeholder object to control access to another underlying target object:
3 Types of Proxies:
1
Virtual Proxy: Delays expensive object initialization until actually accessed (Lazy Loading).
2
Protection Proxy: Checks authorization permissions before forwarding requests.
3
Caching Proxy: Intercepts requests to return cached results.
Code Implementation Example
typescript
interface Image { display(): void; }
class RealImage implements Image {
constructor(private filename: string) { this.loadFromDisk(); }
private loadFromDisk() { console.log('Loading heavy image...'); }
display() { console.log('Displaying image'); }
}
// Virtual Proxy (Lazy Loading)
class ProxyImage implements Image {
private realImage?: RealImage;
constructor(private filename: string) {}
display() {
if (!this.realImage) this.realImage = new RealImage(this.filename);
this.realImage.display();
}
}💡Lazy loading heavy resources using a Virtual Proxy.
🎯 Interview Key Takeaway
Use the Proxy pattern to control access, perform lazy initialization, or cache results for target objects.
#Proxy#Design Patterns#Structural#Intermediate
Back to all Software Engineering Principles Questions→