Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Codility
- mysql
- 코딩테스트
- java
- 백준 11723
- 프로그래머스 옹알이 java
- Math.floor()
- sort
- 0으로 채우기
- 백준 16935
- 백준 15661
- 백준 4375
- 알고리즘
- java 내림
- 백준 16927
- 프로그래머스 네트워크 java
- 프로그래머스 숫자의 표현 java
- 자바
- java 올림
- Math.ceil()
- 프로그래머스 연속된 수의 합 java
- 프로그래머스 도둑질 java
- time complexity
- Algorithm
- 백준 18290
- 백준 17425
- java 반올림
- 백준 14391
- Arrays
- 네트워크
Archives
- Today
- Total
취미처럼
[DP] 데코레이터 패턴 본문
데코레이터 패턴(decorator pattern)
주어진 상황 및 용도에 따라 어떤 객체에 책임을 덧붙이는 패턴으로, 기능 확장이 필요할 때 서브클래싱 대신 쓸 수 있는 유연한 대안이 될 수 있다.
데코레이터 패턴은 프록시 패턴과 구현 방법이 같다.
차이점은 프록시 패턴은 클라이언트가최종적으로 돌려 받는 반환값을 조작하지 않고 그대로 전달하는 반면, 데코레이터 패턴은 클라이언트가 받는 반환값에 장식을 덧입힌다.
// the Window interface
interface Window {
public void draw(); // draws the Window
public String getDescription(); // returns a description of the Window
}
// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
public void draw() {
// draw window
}
public String getDescription() {
return "simple window";
}
}
아래의 클래스들은 모든 Window 클래스들의 데코레이터를 포함하고 있다.
// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
protected Window decoratedWindow; // the Window being decorated
public WindowDecorator (Window decoratedWindow) {
this.decoratedWindow = decoratedWindow;
}
}
// the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
public VerticalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
public void draw() {
drawVerticalScrollBar();
decoratedWindow.draw();
}
private void drawVerticalScrollBar() {
// draw the vertical scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including vertical scrollbars";
}
}
// the second concrete decorator which adds horizontal scrollbar functionality
class HorizontalScrollBarDecorator extends WindowDecorator {
public HorizontalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
public void draw() {
drawHorizontalScrollBar();
decoratedWindow.draw();
}
private void drawHorizontalScrollBar() {
// draw the horizontal scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including horizontal scrollbars";
}
}
Window 인스턴스를 만드는 테스트 프로그램
public class DecoratedWindowTest {
public static void main(String[] args) {
// create a decorated Window with horizontal and vertical scrollbars
Window decoratedWindow = new HorizontalScrollBarDecorator (
new VerticalScrollBarDecorator(new SimpleWindow()));
// print the Window's description
System.out.println(decoratedWindow.getDescription());
}
}
'Design Pattern' 카테고리의 다른 글
[DP] 어댑터 패턴 (0) | 2021.03.19 |
---|---|
[DP] 빌더 패턴 (0) | 2021.03.19 |
[DP] 프록시 패턴 (0) | 2021.03.19 |
[DP] 옵저버 패턴 (0) | 2021.03.19 |
[DP] 전략 패턴 (0) | 2021.03.19 |
Comments