-
The
Subject
:
public interface Logger {
void log();
}
-
The
Real Subject
:
public class FileLogger implements Logger {
public void log() {
System.out.println("FileLogger-log");
}
}
-
The
Proxy
:
public class ProxyFileLogger implements Logger {
private final Logger logger = new FileLogger();
public void log() {
logger.log();
}
}
-
A simple class to test the
Proxy
design pattern:
public class ProxyPatternTest {
public static void main(String[] args) {
// create the proxy
Logger logger = new ProxyFileLogger();
// call method
logger.log();
}
}