Implementing the Proxy Pattern in JavaScript
The Proxy pattern provides a surrogate or placeholder for another object to control access to it. This is useful for adding functionality like access control, logging, caching, or lazy initialization without modifying the original object. This challenge asks you to implement the Proxy pattern in JavaScript to control access to a Subject object.
Problem Description
You are tasked with creating a Proxy class that acts as an intermediary for a Subject class. The Subject class has a method getData() that returns some data. The Proxy should intercept calls to getData() and perform an action before calling the original method. Specifically, the Proxy should log a message to the console indicating that getData() is being called, and then call the getData() method on the Subject.
Key Requirements:
- Create a
Subjectclass with agetData()method. - Create a
Proxyclass that takes aSubjectinstance in its constructor. - The
Proxyclass should have agetData()method that intercepts calls to theSubject'sgetData()method. - The
Proxy'sgetData()method must log a message to the console before calling theSubject'sgetData()method. - The
Proxy'sgetData()method must return the value returned by theSubject'sgetData()method.
Expected Behavior:
When getData() is called on the Proxy instance, the following should happen:
- A message "getData() called via Proxy" should be logged to the console.
- The
getData()method of theSubjectinstance should be called. - The value returned by the
Subject'sgetData()method should be returned by theProxy'sgetData()method.
Edge Cases to Consider:
- What happens if the
Subject'sgetData()method throws an error? TheProxyshould not mask the error; it should propagate it. - Consider how the
Proxyhandles other methods of theSubject. For this challenge, we only focus ongetData(), but think about how you could extend theProxyto handle other methods.
Examples
Example 1:
Input:
const subject = new Subject("Initial Data");
const proxy = new Proxy(subject);
Output:
"getData() called via Proxy" (logged to console)
"Initial Data" (returned from proxy.getData())
Explanation: The proxy.getData() call triggers the logging message and then calls the subject.getData() method, returning the data.
Example 2:
Input:
const subject = new Subject();
subject.data = "Error Data";
subject.getData = function() { throw new Error("Simulated Error"); };
const proxy = new Proxy(subject);
Output:
"getData() called via Proxy" (logged to console)
Error: Simulated Error (thrown from proxy.getData())
Explanation: The proxy.getData() call triggers the logging message, then attempts to call the modified subject.getData() which throws an error. The error is not caught by the proxy and is propagated.
Constraints
- The solution must be written in JavaScript.
- The code should be well-structured and easy to understand.
- The
Proxyclass should not directly modify theSubjectobject. - The
Proxyshould not introduce any unnecessary dependencies.
Notes
- Think about how to use the prototype chain to efficiently handle method interception.
- Consider the potential for extending the
Proxyto handle multiple methods of theSubject. - This is a simplified example of the Proxy pattern. In real-world scenarios, proxies can be much more complex and handle a wider range of responsibilities.
- The focus is on demonstrating the basic interception and forwarding mechanism of the Proxy pattern.