This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| design_pattern:proxy_pattern [2018/01/29 13:51] – ledyx | design_pattern:proxy_pattern [2022/06/14 17:20] (current) – [Proxy Pattern] ledyx | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| = Proxy Pattern = | = Proxy Pattern = | ||
| - | Lazy loading. | + | 본인이 필요해질 때까지 대리인(Proxy)에게 위임. 본인에 대한 접근(Access)를 줄여주는 패턴. |
| - | {{tag> | + | * **<fc red> |
| + | * Authorization | ||
| + | * Caching | ||
| + | * Lazy loading | ||
| + | * [[design_pattern: | ||
| + | |||
| + | {{tag> | ||
| = Virtual Proxy = | = Virtual Proxy = | ||
| - | Instance가 필요한 시점에서 생성 및 초기화를 실행. 필요한 객체만 생성하여 초기화시 속도 향상. | + | Instance가 필요한 시점에서 생성 및 초기화를 실행. 필요한 객체만 생성하여 초기화시 속도 향상. |
| + | |||
| + | == API == | ||
| + | === Subject === | ||
| + | <sxh java> | ||
| + | public interface Printable { | ||
| + | void setPrinterName(String name); | ||
| + | String getPrinterName(); | ||
| + | void print(String string); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Proxy === | ||
| + | <sxh java> | ||
| + | package core; | ||
| + | |||
| + | public class PrinterProxy implements Printable { | ||
| + | |||
| + | private String name; | ||
| + | private Printer real; | ||
| + | |||
| + | public PrinterProxy() { | ||
| + | } | ||
| + | |||
| + | public PrinterProxy(String name) { | ||
| + | this.name = name; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public synchronized void setPrinterName(String name) { | ||
| + | if(real != null) | ||
| + | real.setPrinterName(name); | ||
| + | |||
| + | this.name = name; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String getPrinterName() { | ||
| + | return name; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void print(String string) { | ||
| + | realize(); | ||
| + | real.print(string); | ||
| + | } | ||
| + | |||
| + | private synchronized void realize() { | ||
| + | if(real == null) | ||
| + | real = new Printer(name); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | === RealSubject === | ||
| + | <sxh java> | ||
| + | public class Printer implements Printable { | ||
| + | |||
| + | private String name; | ||
| + | |||
| + | public Printer(String name) { | ||
| + | this.name = name; | ||
| + | heavyJob(" | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void setPrinterName(String name) { | ||
| + | this.name = name; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public String getPrinterName() { | ||
| + | return name; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public void print(String string) { | ||
| + | System.out.println(name + " / " + string); | ||
| + | } | ||
| + | |||
| + | private void heavyJob(String message) { | ||
| + | System.out.println(message); | ||
| + | |||
| + | for(int i=0 ; i<5 ; i++) { | ||
| + | try { | ||
| + | Thread.sleep(1000); | ||
| + | } catch (InterruptedException e) { | ||
| + | } | ||
| + | System.out.print(" | ||
| + | } | ||
| + | |||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Client == | ||
| + | <sxh java> | ||
| + | public class Client { | ||
| + | public static void main(String[] args) { | ||
| + | Printable p = new PrinterProxy(" | ||
| + | System.out.println(" | ||
| + | |||
| + | p.setPrinterName(" | ||
| + | System.out.println(" | ||
| + | |||
| + | // 대리자가 처리할 수 없는 무거운 작업. 이 때, RealSubject가 필요해지는 시점. (Lazy loading) | ||
| + | p.print(" | ||
| + | } | ||
| + | } | ||
| + | /* | ||
| + | Who are you? Luke | ||
| + | Who are you? Michael | ||
| + | Printer의 instance Michael 생성 중 | ||
| + | .....완료! | ||
| + | Michael / Hello, world! | ||
| + | */ | ||
| + | </ | ||
| = Remote Proxy = | = Remote Proxy = | ||