프로그래밍/자바 디자인 패턴
자바 디자인 패턴 튜토리얼 - 구조 설계 패턴 - 프록시 패턴
Ericlee
2021. 4. 18. 02:35
반응형
프록시 패턴에서 클래스는 다른 클래스의 기능을 나타냅니다.
프록시 패턴은 구조 패턴입니다.
프록시 패턴에서는 원래의 인터페이스로 객체를 생성하여 기능을 외부 세계에 노출합니다.
예제
interface Printer {
void print();
}
class ConsolePrinter implements Printer {
private String fileName;
public ConsolePrinter(String fileName){
this.fileName = fileName;
}
@Override
public void print() {
System.out.println("Displaying " + fileName);
}
}
class ProxyPrinter implements Printer{
private ConsolePrinter consolePrinter;
private String fileName;
public ProxyPrinter(String fileName){
this.fileName = fileName;
}
@Override
public void print() {
if(consolePrinter == null){
consolePrinter = new ConsolePrinter(fileName);
}
consolePrinter.print();
}
}
public class Main {
public static void main(String[] args) {
Printer image = new ProxyPrinter("test");
image.print();
}
}
위의 코드는 다음 결과를 출력합니다
Displaying test
반응형