Hone logo
Hone
Problems

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 Subject class with a getData() method.
  • Create a Proxy class that takes a Subject instance in its constructor.
  • The Proxy class should have a getData() method that intercepts calls to the Subject's getData() method.
  • The Proxy's getData() method must log a message to the console before calling the Subject's getData() method.
  • The Proxy's getData() method must return the value returned by the Subject's getData() method.

Expected Behavior:

When getData() is called on the Proxy instance, the following should happen:

  1. A message "getData() called via Proxy" should be logged to the console.
  2. The getData() method of the Subject instance should be called.
  3. The value returned by the Subject's getData() method should be returned by the Proxy's getData() method.

Edge Cases to Consider:

  • What happens if the Subject's getData() method throws an error? The Proxy should not mask the error; it should propagate it.
  • Consider how the Proxy handles other methods of the Subject. For this challenge, we only focus on getData(), but think about how you could extend the Proxy to 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 Proxy class should not directly modify the Subject object.
  • The Proxy should 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 Proxy to handle multiple methods of the Subject.
  • 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.
Loading editor...
javascript