The Proxy pattern is a structural design pattern that allows you to provide a surrogate or placeholder for another object to control access to it. In Java, it is often used to control access to an object that is resource-intensive to create or where access should be restricted for security reasons.
The Proxy pattern involves three main components:
// Subject interface
interface Subject {
void request();
}
// RealSubject class
class RealSubject implements Subject {
@Override
public void request() {
System.out.println("RealSubject: Handling request.");
}
}
// Proxy class
class Proxy implements Subject {
private RealSubject realSubject;
@Override
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
// Additional functionality can be added here if necessary
realSubject.request();
}
}
public class Client {
public static void main(String[] args) {
Subject proxy = new Proxy();
proxy.request(); // First call, creates RealSubject
proxy.request(); // Second call, uses cached RealSubject
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?