๋””์ž์ธ ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ํ›‘์–ด๋ณด๊ธด ํ–ˆ์—ˆ๋Š”๋ฐ ๋‹ค์‹œํ•œ๋ฒˆ ๋ณต์Šตํ•˜๋Š” ์ฐจ์›์—์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ์ •๋ฆฌํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ–๊ณ  ์‹ถ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜์€ ๊ทธ ์ฒซ์งธ ๋‚ .

 

์˜ค ๋Š˜ ํ•จ๊ป˜ ๋ณผ ๋””์ž์ธํŒจํ„ด์€ Iterator ํŒจํ„ด์ด๋‹ค. ๋‚ด์šฉ์˜ ๋Œ€๋ถ€๋ถ„์€ GoF์˜ ๋””์ž์ธํŒจํ„ด ์ฑ…์—์„œ ๋ฐœ์ทŒ๋ฅผ ํ•˜๊ฒŒ๋  ๊ฒƒ์ž„์„ ๋ฐํžŒ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜๋ฌธ์„ ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญ์„ ํ•˜๊ฒŒ ๋ ํ…๋ฐ ์ „๋ฌธ๋ฒˆ์—ญ๊ฐ€๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ํ•œ๊ธ€ ๋ฒˆ์—ญ๋ณธ์ด ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ„๋‹ค๊ณ  ๋Š๋ผ๋ฉด ์˜๋ฌธ ์›๋ณธ์„ ์ฝ์–ด๋ณด์‹œ๊ธธ ๊ถŒ์œ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ด ๋ณผ๊นŒ?

 

์šฐ์„  GoF์˜ ๋””์ž์ธ ํŒจํ„ด์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํฌ๊ฒŒ 3๊ฐ€์ง€ ์ข…๋ฅ˜๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค.

 Creational Patterns ( ์ƒ์„ฑ ํŒจํ„ด )

These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case. 

์ƒ์„ฑํŒจํ„ด์€ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๋กœ์ง์„ ์ˆจ๊ธฐ๊ณ  new ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์„ ์ œ๊ณตํ•œ๋‹ค. ์ด๋Š” ํŠน์ • ์ƒํ™ฉ์—์„œ ์–ด๋–ค ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค. 


 Structural Patterns ( ๊ตฌ์กฐ์  ํŒจํ„ด )

These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities. 

๊ตฌ์กฐ์  ํŒจํ„ด๋“ค์€ ํด๋ž˜์Šค์™€ ๊ฐ์ฒด์˜ ๊ตฌ์„ฑ์— ๊ด€์—ฌํ•œ๋‹ค.

 

 Behavioral Patterns ( ํ–‰์œ„์  ํŒจํ„ด )

These design patterns are specifically concerned with communication between objects.

์ด ํŒจํ„ด๋“ค์€ ๊ฐ์ฒด๋“ค ์‚ฌ์ด์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ๊ด€์‹ฌ์„ ๊ฐ€์ง„๋‹ค.

 

์ด์ œ ์‚ดํŽด๋ณผ Iterator ํŒจํ„ด์€ Behavioral ํŒจํ„ด์— ์†ํ•œ๋‹ค. 

Iterator๋Š” ์ž๋ฐ”์˜ Iterator์™€ ๊ฐ™์€ ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์‚ฌ์ „์  ์˜๋ฏธ๋กœ "๋ฐ˜๋ณต์ž"๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” iterator๋Š” ์ž๋ฐ”์˜ ๊ฒฝ์šฐ collection๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ์ด ๋œ๋‹ค. ์˜ค๋ผํด์—์„œ ์ œ๊ณตํ•˜๋Š” Iterator์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์ด๊ณณ์—์„œ ํ™•์ธํ•˜๋ฉด๋œ๋‹ค. ์ด์ฒ˜๋Ÿผ ์•ž์œผ๋กœ ์‚ดํŽด๋ณผ ๋ชจ๋“  ํŒจํ„ด๋“ค์€ ๊ทธ ์ด๋ฆ„์—์„œ ์ด ํŒจํ„ด์ด ์–ด๋–ค ํŒจํ„ด์ธ์ง€ ํŒŒ์•…ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

Iterator Pattern Structure

 

 


ํŒจํ„ด์˜ ๋ชฉ์  :
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

 

Iterator ํŒจํ„ด์˜ ๋ชฉ์ ์€ aggregate object ( ex. List )๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌผ๋ก  ์ด ๊ณผ์ •์—์„œ ๊ฐœ๋ฐœ์ž๋Š” object๊ฐ€ ์–ด๋–ค์‹์œผ๋กœ ๊ตฌํ˜„์ด ๋˜์–ด์žˆ๋Š”์ง€๋Š” ์•Œ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ object์˜ ๊ตฌํ˜„๋ถ€๋ฅผ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.

 

์œ ์šฉ์„ฑ ( Applicability ) :

ยท to access an aggregate object's contents without exposing its internal representation.

๋‚ด๋ถ€ ๊ตฌํ˜„์„ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด์„œ ์ง‘์•ฝ ๊ฐ์ฒด์˜ ๋‚ด์šฉ๋ฌผ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ
ยท to support multiple traversals of aggregate objects.

์ง‘์•ฝ(์ง‘ํ•ฉ) ๊ฐ์ฒด๋“ค์— ๋Œ€ํ•ด ๋‹ค์–‘ํ•œ ํƒ์ƒ‰๊ฒฝ๋กœ(?)๋ฅผ ์ง€์›ํ•˜๊ณ ์ž ํ•  ๋•Œ
ยท to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).

๋‹ค๋ฅธ ์ง‘์•ฝ(์ง‘ํ•ฉ)๊ตฌ์กฐ๋“ค์„ ํƒ์ƒ‰ํ•˜๊ธฐ์œ„ํ•œ ํ†ต์ผ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ ์ž ํ•  ๋•Œ (์ฆ‰, ๋‹คํ˜•์  iteration์„ ์ง€์›ํ•˜๊ณ ์ž ํ•  ๋•Œ )

 

๋“ฑ์žฅ ์ธ๋ฌผ :
ยท Iterator
o defines an interface for accessing and traversing elements.
ยท ConcreteIterator
o implements the Iterator interface.
o keeps track of the current position in the traversal of the aggregate.
ยท Aggregate
o defines an interface for creating an Iterator object.
ยท ConcreteAggregate
o implements the Iterator creation interface to return an instance of the proper ConcreteIterator.

 

์žฅ๋‹จ์  :
1. It supports variations in the traversal of an aggregate.Complex aggregates
may be traversed in many ways. For example, code generation and semantic checking involve traversing parse trees. Code generation may traverse the parse tree inorder or preorder.Iterators make it easy to change the traversal algorithm: Just replace the iterator instance with a different
one. You can also define Iterator subclasses to support new traversals.
2. Iterators simplify the Aggregate interface.Iterator's traversal interface obviates the need for a similar interface in Aggregate, thereby simplifying the aggregate's interface.
3. More than one traversal can be pending on an aggregate.An iterator keeps track of its own traversal state. Therefore you can have more than one traversal in progress at once.


๊ด€๋ จ ํŒจํ„ด๋“ค : 

Composite : Iterators are often applied to recursive structures such as Composites.
Factory Method : Polymorphic iterators rely on factory methods to instantiate theappropriate Iterator subclass.
Memento is often used in conjunction with the Iterator pattern. An iterator can use a memento to capture the state of an iteration. The iterator stores the memento internally. 

 

 

 


์ถœ์ฒ˜ : Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )