The Proxy pattern is a design pattern where a proxy object controls access to another object, called the real subject. It acts as a placeholder to add extra functionality like access control, lazy loading, or logging without changing the real object's code.
Click to reveal answer
intermediate
Name three common types of proxies used in system design.
1. Virtual Proxy: Creates expensive objects only when needed. 2. Protection Proxy: Controls access rights to the real object. 3. Remote Proxy: Represents an object in a different address space or machine.
Click to reveal answer
intermediate
How does a Proxy pattern improve system scalability?
By delaying expensive object creation (lazy loading) or controlling access, proxies reduce resource use and improve performance. For example, a virtual proxy creates objects only when needed, saving memory and CPU.
Click to reveal answer
beginner
In the Proxy pattern, what role does the proxy object play compared to the real subject?
The proxy object acts as a middleman. It has the same interface as the real subject and forwards requests to it. It can add extra behavior before or after forwarding, like checking permissions or caching results.
Click to reveal answer
beginner
Give a real-life analogy for the Proxy pattern.
Think of a receptionist (proxy) in an office who controls visitor access to the manager (real subject). The receptionist checks visitor credentials and only lets authorized people meet the manager, adding a layer of control.
Click to reveal answer
Which of the following best describes a Virtual Proxy?
ADuplicates the real object to improve performance
BControls user permissions to access an object
CCreates expensive objects only when they are needed
DRepresents an object located on a remote server
✗ Incorrect
A Virtual Proxy delays the creation of expensive objects until they are actually needed.
What is the main purpose of a Protection Proxy?
ATo control access rights to the real object
BTo create objects lazily
CTo cache results for faster access
DTo represent remote objects
✗ Incorrect
A Protection Proxy controls who can access the real object, enforcing security or permissions.
In the Proxy pattern, the proxy object must have the same ______ as the real subject.
Amemory size
Binterface
Cimplementation
Dlocation
✗ Incorrect
The proxy must implement the same interface so it can be used interchangeably with the real subject.
Which proxy type is used to represent an object in a different address space or machine?
AVirtual Proxy
BSmart Proxy
CProtection Proxy
DRemote Proxy
✗ Incorrect
A Remote Proxy represents an object located remotely, handling communication details.
What benefit does the Proxy pattern provide in system design?
AIt adds control and extra behavior without changing the real object
BIt hides the real object completely
CIt duplicates objects to improve speed
DIt removes the need for interfaces
✗ Incorrect
The Proxy pattern adds control or extra behavior transparently without modifying the real object.
Explain the Proxy pattern and describe a scenario where it can be useful.
Think about how a middleman can help control or delay access.
You got /3 concepts.
List and explain the differences between Virtual Proxy, Protection Proxy, and Remote Proxy.
Focus on what each proxy controls or represents.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of the Proxy pattern in system design?
easy
A. To replace the real object permanently with a new one
B. To control access to an object by using a placeholder
C. To directly modify the real object's data
D. To create multiple instances of an object
Solution
Step 1: Understand the Proxy pattern role
The Proxy pattern acts as a stand-in or placeholder to control access to another object.
Step 2: Compare options with the pattern definition
Only To control access to an object by using a placeholder correctly describes controlling access using a placeholder, which matches the Proxy pattern.
Final Answer:
To control access to an object by using a placeholder -> Option B
Quick Check:
Proxy pattern = control access placeholder [OK]
Hint: Proxy controls access via placeholder, not object creation [OK]
Common Mistakes:
Confusing Proxy with Singleton pattern
Thinking Proxy modifies the real object directly
Assuming Proxy replaces the real object permanently
2.
Which of the following is the correct way to declare a proxy class that implements the same interface as the real object?
interface Image {
void display();
}
// Which proxy class declaration is correct?
easy
A. interface ImageProxy implements Image { void display(); }
B. class ImageProxy extends Image { void display() { /* control access */ } }
C. class ImageProxy { void display() { /* control access */ } }
D. class ImageProxy implements Image { void display() { /* control access */ } }
Solution
Step 1: Recall interface implementation syntax
In many languages, a class implements an interface using the 'implements' keyword, not 'extends'.
Step 2: Check each option's correctness
class ImageProxy implements Image { void display() { /* control access */ } } correctly declares a class implementing the interface. class ImageProxy extends Image { void display() { /* control access */ } } wrongly uses 'extends' for an interface. class ImageProxy { void display() { /* control access */ } } misses interface implementation. interface ImageProxy implements Image { void display(); } wrongly declares an interface implementing another interface.
Final Answer:
class ImageProxy implements Image { void display() { /* control access */ } } -> Option D
Quick Check:
Proxy class implements interface = class ImageProxy implements Image { void display() { /* control access */ } } [OK]
Hint: Proxy class must implement interface, not extend it [OK]
Common Mistakes:
Using 'extends' instead of 'implements' for interfaces
Not implementing the interface at all
Declaring proxy as an interface instead of a class
3.
Given the following code snippet, what will be the output?
class RealImage {
void display() { System.out.println("Displaying real image"); }
}
class ProxyImage {
RealImage realImage;
void display() {
if (realImage == null) {
realImage = new RealImage();
}
realImage.display();
}
}
ProxyImage proxy = new ProxyImage();
proxy.display();
proxy.display();
medium
A. Displaying real image
Displaying real image
B. Displaying real image
C. No output
D. NullPointerException
Solution
Step 1: Analyze the first call to display()
Since realImage is null initially, it is created and then display() prints "Displaying real image".
Step 2: Analyze the second call to display()
realImage is no longer null, so display() is called directly again, printing the same message.
Final Answer:
Displaying real image
Displaying real image -> Option A
Quick Check:
Lazy loading calls display twice [OK]
Hint: Proxy lazy loads real object, so display prints twice [OK]
Common Mistakes:
Assuming display prints only once
Expecting NullPointerException due to uninitialized realImage
Thinking proxy suppresses output
4.
Identify the error in the following proxy implementation and select the fix:
The proxy calls real.request() but 'real' is never initialized, causing a NullPointerException.
Step 2: Determine the correct fix
Initializing 'real' before calling request() ensures the real object exists and avoids errors.
Final Answer:
Initialize 'real' before calling request() -> Option C
Quick Check:
Uninitialized real causes error; initialize it [OK]
Hint: Always initialize real object before proxy calls it [OK]
Common Mistakes:
Ignoring null initialization error
Changing method names unnecessarily
Removing proxy defeats pattern purpose
5.
You are designing a system where loading a large image file is expensive. You want to use the Proxy pattern to improve performance by loading the image only when needed. Which of the following design choices best applies the Proxy pattern for this scenario?
hard
A. Create a proxy class that holds the image filename and loads the real image only when display() is called
B. Load the real image immediately when the proxy is created to avoid delays
C. Use a proxy that duplicates the image data to speed up access
D. Replace the real image with a smaller thumbnail permanently
Solution
Step 1: Understand the problem context
Loading large images is expensive, so delaying loading until necessary improves performance.
Step 2: Match design choices to Proxy pattern benefits
Create a proxy class that holds the image filename and loads the real image only when display() is called uses lazy loading by deferring real image loading until display() is called, which fits Proxy pattern use.
Final Answer:
Create a proxy class that holds the image filename and loads the real image only when display() is called -> Option A
Quick Check:
Lazy loading with proxy = Create a proxy class that holds the image filename and loads the real image only when display() is called [OK]
Hint: Proxy delays expensive load until needed (lazy loading) [OK]
Common Mistakes:
Loading real object immediately defeats proxy benefit
Duplicating data wastes resources
Replacing with thumbnail changes object behavior
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.