Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
41
02-structural/proxy/LoggingProxy.java
Normal file
41
02-structural/proxy/LoggingProxy.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package proxy;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* Logging Proxy — adds timing and audit logging around every query
|
||||
* without touching RealDatabaseConnection or any of its callers.
|
||||
*
|
||||
* This is the "cross-cutting concern" use case of Proxy,
|
||||
* the same mechanism behind Spring AOP's @Around advice.
|
||||
*/
|
||||
public class LoggingProxy implements DatabaseConnection {
|
||||
|
||||
private final DatabaseConnection target;
|
||||
|
||||
public LoggingProxy(DatabaseConnection target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
System.out.println("[Log] connect() at " + Instant.now());
|
||||
target.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeQuery(String sql) {
|
||||
long start = System.currentTimeMillis();
|
||||
System.out.println("[Log] QUERY START: " + sql);
|
||||
String result = target.executeQuery(sql);
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
System.out.println("[Log] QUERY END: " + elapsed + "ms | result: " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
System.out.println("[Log] disconnect() at " + Instant.now());
|
||||
target.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user