반응형
프록시 패턴에서 클래스는 다른 클래스의 기능을 나타냅니다.
프록시 패턴은 구조 패턴입니다.
프록시 패턴에서는 원래의 인터페이스로 객체를 생성하여 기능을 외부 세계에 노출합니다.
예제
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
반응형
'프로그래밍 > 자바 디자인 패턴' 카테고리의 다른 글
자바 디자인 패턴 튜토리얼 - 구조 설계 패턴 - 데코레이터 패턴 (0) | 2021.04.18 |
---|---|
자바 디자인 패턴 튜토리얼 - 구조 설계 패턴 - 퍼사드 패턴 (0) | 2021.04.18 |
자바 디자인 패턴 튜토리얼 - 행동 디자인 패턴 - 책임 연쇄 패턴 (0) | 2021.04.18 |
자바 디자인 패턴 튜토리얼 - 행동 디자인 패턴 - 자바 명령 패턴 (0) | 2021.04.18 |
자바 디자인 패턴 튜토리얼 - 행동 디자인 패턴 - 반복자 패턴 (0) | 2021.04.18 |