Junior — Middle
Are you creating a new class when implementing object proxying?
sobes.tech AI
Answer from AI
Yes, when implementing object proxying, a new class — a proxy — is usually created, which wraps the original object. This proxy class intercepts method calls, can add additional logic (such as lazy initialization, logging, access control), and then delegates the calls to the original object.
Example of a simple proxy in Java:
public interface Service {
void perform();
}
public class RealService implements Service {
public void perform() {
System.out.println("Executing main logic");
}
}
public class ServiceProxy implements Service {
private RealService realService;
public ServiceProxy(RealService realService) {
this.realService = realService;
}
public void perform() {
System.out.println("Before execution");
realService.perform();
System.out.println("After execution");
}
}
Here, ServiceProxy is a new class that implements proxying of the RealService object.