Skip to content

Commit 961121c

Browse files
authored
translation : Translate the Decorater to Korean (#2784)
1 parent e340f4f commit 961121c

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
title: Decorator
3+
category: Structural
4+
language: en
5+
tag:
6+
- Gang of Four
7+
- Extensibility
8+
---
9+
10+
## ~로 알려져 있는
11+
12+
Wrapper
13+
14+
## 의도
15+
16+
객체에 동적으로 추가적인 책임을 부여합니다. 데코레이터(Decorater)는 기능 확장을 위한 서브 클래스를 만드는 것의
17+
유연한 대안을 제공합니다.
18+
19+
## 설명
20+
21+
실제 예제
22+
23+
> 근처 언덕에 성난 트롤이 살고 있습니다. 보통은 맨손으로 다니지만 가끔씩은 무기를 가지고 있습니다.
24+
> 트롤을 무장시키기 위해 새로운 트롤을 만들 필요 없이 적절한 무기로 트롤을 장식(decorate)하면 됩니다.
25+
26+
쉽게 설명하면
27+
28+
> 데코레이터(Decorator) 패턴은 객체를 데코레이터(Decorater) 클래스의 객체로 감싸 런타임 시,
29+
> 객체의 동작을 동적으로 변경할 수 있도록 합니다.
30+
31+
Wikipedia에 의하면
32+
33+
> 객체 지향 프로그래밍에서, 데코레이터(Decorater)은 다음과 같은 동작을 허용하는 디자인 패턴입니다.
34+
> 정적, 동적 상관없이 개별 객체에 동작을 추가할 수 있는 디자인 패턴입니다.
35+
> 동작에 영향을 주지 않고 개별 객체에 동작을 추가할 수 있는 디자인 패턴입니다.
36+
> 데코레이터(Decorater) 패턴은 종종 단일 책임 원칙(SRP)을 준수하는데 유용합니다.
37+
> 고유한 관심 영역을 가진 클래스 간에 기능을 나눌 수 있기 때문에 개방 폐쇄 원칙(OCP)를 준수하는데 유용합니다.
38+
> 클래스의 기능을 수정하지 않고 확장할 수 있기 때문입니다.
39+
40+
**프로그램 코드 예제**
41+
42+
트롤을 예로 들어보겠습니다. 먼저, 'Troll' 인터페이스를 구현하는 'SimpleTroll'이 있습니다.
43+
44+
```java
45+
public interface Troll {
46+
void attack();
47+
int getAttackPower();
48+
void fleeBattle();
49+
}
50+
51+
@Slf4j
52+
public class SimpleTroll implements Troll {
53+
54+
@Override
55+
public void attack() {
56+
LOGGER.info("The troll tries to grab you!");
57+
}
58+
59+
@Override
60+
public int getAttackPower() {
61+
return 10;
62+
}
63+
64+
@Override
65+
public void fleeBattle() {
66+
LOGGER.info("The troll shrieks in horror and runs away!");
67+
}
68+
}
69+
```
70+
71+
다음으로, 트롤을 위한 곤봉을 추가하겠습니다. 데코레이터(Decorater)를 사용해 동적으로 추가할 수 있습니다.
72+
73+
```java
74+
@Slf4j
75+
public class ClubbedTroll implements Troll {
76+
77+
private final Troll decorated;
78+
79+
public ClubbedTroll(Troll decorated) {
80+
this.decorated = decorated;
81+
}
82+
83+
@Override
84+
public void attack() {
85+
decorated.attack();
86+
LOGGER.info("The troll swings at you with a club!");
87+
}
88+
89+
@Override
90+
public int getAttackPower() {
91+
return decorated.getAttackPower() + 10;
92+
}
93+
94+
@Override
95+
public void fleeBattle() {
96+
decorated.fleeBattle();
97+
}
98+
}
99+
```
100+
101+
트롤이 실제로 동작하는 모습입니다.
102+
103+
```java
104+
// simple troll
105+
LOGGER.info("A simple looking troll approaches.");
106+
var troll = new SimpleTroll();
107+
troll.attack();
108+
troll.fleeBattle();
109+
LOGGER.info("Simple troll power: {}.\n", troll.getAttackPower());
110+
111+
// change the behavior of the simple troll by adding a decorator
112+
LOGGER.info("A troll with huge club surprises you.");
113+
var clubbedTroll = new ClubbedTroll(troll);
114+
clubbedTroll.attack();
115+
clubbedTroll.fleeBattle();
116+
LOGGER.info("Clubbed troll power: {}.\n", clubbedTroll.getAttackPower());
117+
```
118+
119+
프로그램 실행 결과:
120+
121+
```java
122+
A simple looking troll approaches.
123+
The troll tries to grab you!
124+
The troll shrieks in horror and runs away!
125+
Simple troll power: 10.
126+
127+
A troll with huge club surprises you.
128+
The troll tries to grab you!
129+
The troll swings at you with a club!
130+
The troll shrieks in horror and runs away!
131+
Clubbed troll power: 20.
132+
```
133+
134+
## 클래스 다이어그램
135+
136+
![alt text](./etc/decorator.urm.png "Decorator pattern class diagram")
137+
138+
## 적용 가능성
139+
140+
다음과 같은 경우 데코레이터(Decorator) 패턴을 사용합니다.:
141+
142+
* 다른 객체에 영향을 주지 않으면서 개별 객체에 동적으로 투명하게 책임을 추가할 수 있습니다.
143+
* 철회될 수 있는 책임들의 경우에 사용합니다.
144+
* 서브클래스를 통한 확장이 실용적이지 않을 경우에 사용합니다. 때로 많은 수의 독립적인 확장은 모든 조합을 지원하기 위해 서브클래스가 폭발적으로 늘어날 수 있습니다.
145+
또는 클래스 정의가 숨겨져 있거나 서브클래스 생성이 불가능한 경우에 사용합니다.
146+
147+
## 튜토리얼
148+
149+
* [Decorator Pattern Tutorial](https://www.journaldev.com/1540/decorator-design-pattern-in-java-example)
150+
151+
## 실제 사례
152+
153+
* [java.io.InputStream](http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html), [java.io.OutputStream](http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html),
154+
[java.io.Reader](http://docs.oracle.com/javase/8/docs/api/java/io/Reader.html), [java.io.Writer](http://docs.oracle.com/javase/8/docs/api/java/io/Writer.html)
155+
* [java.util.Collections#synchronizedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedCollection-java.util.Collection-)
156+
* [java.util.Collections#unmodifiableXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableCollection-java.util.Collection-)
157+
* [java.util.Collections#checkedXXX()](http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#checkedCollection-java.util.Collection-java.lang.Class-)
158+
159+
160+
## 크레딧
161+
162+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
163+
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](https://www.amazon.com/gp/product/1937785467/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1937785467&linkCode=as2&tag=javadesignpat-20&linkId=7e4e2fb7a141631491534255252fd08b)
164+
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
165+
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
166+
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
167+
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)
23 KB
Loading

0 commit comments

Comments
 (0)