Proxy Pattern

ν”„λ‘μ‹œ νŒ¨ν„΄

ν”„λ‘μ‹œ νŒ¨ν„΄μ€ λ§κ·ΈλŒ€λ‘œ 객체λ₯Ό λŒ€μ‹ ν•΄μ„œ λŒ€λ¦¬μžλ₯Ό ν•΄μ€€λ‹€λŠ” μ˜λ―Έμ΄λ‹€.

ν”„λ‘μ‹œ νŒ¨ν„΄μ„ μ‚¬μš©ν•˜κ²Œ 되면 ν”„λ‘μ‹œ λ‹¨κ³„μ—μ„œ κΆŒν•œλΆ€μ—¬λ₯Ό ν• μˆ˜ μžˆλŠ” 이점이 생기고 객체λ₯Ό μƒμ„±μ‹œν‚€κ±°λ‚˜ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— λ©”λͺ¨λ¦¬λ₯Ό μ ˆμ•½ν•  수 μžˆλŠ” 이점도 생긴닀.

μžμ‹ μ΄ λ³΄ν˜Έν•˜κ³  μžˆλŠ” 객체에 λŒ€ν•œ μ•‘μ„ΈμŠ€ κΆŒν•œμ„ μ œμ–΄ν•˜λŠ”κ²ƒμ΄λ‹€.

public interface Executor {
  public void run(String command) throws Exception;
}

public class ExecutorImpl implements Excutor {
  @Override
  public void run(String command) throws IOException {
    System.out.println("command");
  }
}
public class ExecutorProxy implements Excutor {

  private Executor exec;

  public ExecutorProxy(String user, String passwd) {
    private boolean isAdmin;
    if ("rrest".equals(user) && "1234".equals(passwd)) {
      isAdmin = true;
    }

    exec = new ExecutorImpl();
  }

  @Override
  public void run(String command) throws Exception {
    if (isAdmin) {
      exec.run(command);
    } else {
      throw new Exception("You are not Administrator !!");
    }
  }
}
public class ProxyPatternTest {
  public static void main(String[] args) {
    Executor exec = new ExecutorProxy("rrest", "1234");
    exec.run("ν”„λ‘μ‹œ ν…ŒμŠ€νŠΈ");
  }
}