Skip to content

Commit cfeee23

Browse files
authored
translation: Closes iluwatar#2162 Translate template-method pattern readme to Korean (iluwatar#2173)
1 parent d9b4965 commit cfeee23

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

localization/ko/iterator/README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: Iterator
3+
categories: Behavioral
4+
language: ko
5+
tags:
6+
- Gang of Four
7+
---
8+
9+
## 또한 ~으로 알려진
10+
11+
커서(Cursor) 패턴
12+
13+
## 의도
14+
기반이되는 표현을 노출하지 않고 Aggregate 객체의 요소에 순차적으로 접근할 수 있는 방법을 제공합니다.
15+
16+
## 설명
17+
18+
실제 예제
19+
20+
> 보물상자에는 마법의 아이템이 들어 있습니다. 반지, 물약, 무기와 같은 다양한 종류의 물건들이 있습니다. 보물상자가 제공하는 반복기(iterator)를 사용하여 아이템은 유형별로 검색될 수 있을 것입니다.
21+
22+
쉽게 말하자면
23+
24+
> 컨테이너는 요소에 대한 접근을 제공하기 위해 표현에 구애받지 않는 반복기(iterator) 인터페이스를 제공할 수 있습니다.
25+
26+
Wikipedia에 의하면
27+
28+
> 반복자 패턴(iterator pattern)은 객체 지향 프로그래밍에서 반복자를 사용하여 컨테이너를 가로지르며 컨테이너의 요소들에 접근하는 디자인 패턴입니다.
29+
30+
**코드 예제**
31+
32+
예제의 메인 클래스는 아이템들을 담고있는 `보물상자(TreasureChest)` 입니다.
33+
34+
```java
35+
public class TreasureChest {
36+
37+
private final List<Item> items;
38+
39+
public TreasureChest() {
40+
items = List.of(
41+
new Item(ItemType.POTION, "Potion of courage"),
42+
new Item(ItemType.RING, "Ring of shadows"),
43+
new Item(ItemType.POTION, "Potion of wisdom"),
44+
new Item(ItemType.POTION, "Potion of blood"),
45+
new Item(ItemType.WEAPON, "Sword of silver +1"),
46+
new Item(ItemType.POTION, "Potion of rust"),
47+
new Item(ItemType.POTION, "Potion of healing"),
48+
new Item(ItemType.RING, "Ring of armor"),
49+
new Item(ItemType.WEAPON, "Steel halberd"),
50+
new Item(ItemType.WEAPON, "Dagger of poison"));
51+
}
52+
53+
public Iterator<Item> iterator(ItemType itemType) {
54+
return new TreasureChestItemIterator(this, itemType);
55+
}
56+
57+
public List<Item> getItems() {
58+
return new ArrayList<>(items);
59+
}
60+
}
61+
```
62+
63+
다음은 `아이템(item)` 클래스입니다. :
64+
65+
```java
66+
public class Item {
67+
68+
private ItemType type;
69+
private final String name;
70+
71+
public Item(ItemType type, String name) {
72+
this.setType(type);
73+
this.name = name;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return name;
79+
}
80+
81+
public ItemType getType() {
82+
return type;
83+
}
84+
85+
public final void setType(ItemType type) {
86+
this.type = type;
87+
}
88+
}
89+
90+
public enum ItemType {
91+
92+
ANY, WEAPON, RING, POTION
93+
94+
}
95+
```
96+
97+
`반복자(Iterator)` 인터페이스는 아주 단순합니다.
98+
99+
```java
100+
public interface Iterator<T> {
101+
102+
boolean hasNext();
103+
104+
T next();
105+
}
106+
```
107+
108+
다음 예제에서는 보물상자에서 발견되는 고리형 항목을 반복한다.
109+
110+
```java
111+
var itemIterator = TREASURE_CHEST.iterator(ItemType.RING);
112+
while (itemIterator.hasNext()) {
113+
LOGGER.info(itemIterator.next().toString());
114+
}
115+
```
116+
117+
프로그램 실행결과:
118+
119+
```java
120+
Ring of shadows
121+
Ring of armor
122+
```
123+
124+
## 클래스 다이어그램
125+
126+
![alt text](../../../iterator/etc/iterator_1.png "Iterator")
127+
128+
## 적용 가능성
129+
130+
템플릿 메소드는 사용되어야합니다.
131+
132+
* 내부 표현을 드러내지않고 aggregate 객체의 컨텐츠에 접근하기 위해서
133+
134+
* aggregate 오브젝트들의 여러 번의 횡단을 지원하기 위해서
135+
136+
* 서로 다른 aggregate 구조들의 횡단을 위한 획일적인 인터페이스를 제공하기 위해서
137+
138+
## 튜토리얼
139+
140+
* [How to Use Iterator?](http://www.tutorialspoint.com/java/java_using_iterator.htm)
141+
142+
## 실제 사례
143+
144+
* [java.util.Iterator](http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html)
145+
* [java.util.Enumeration](http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html)
146+
147+
## 크레딧
148+
149+
* [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)
150+
* [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)

0 commit comments

Comments
 (0)