[CS]:: Design pattern - 이터레이터 패턴 singletone pattern

김종건·2023년 5월 22일

CS 디자인 패턴

목록 보기
6/8
post-thumbnail

🎯이터레이터 패턴

이터레이터(iterator, 반복자)를 사용하여 컬렉션( collection)의 요소들에 접근하는 디자인 패턴입니다. 이를 통해 순회할 수 있는 여러 가지 자료형의 구조와는 상관없이 이터레이터라는 하나의 인터페이스로 순회가 가능

  • 순서가 있는 것들( 수열, 배열 등) 을 편리하게 탐색 할 수 있는 패턴

  • 순서대로 나열되는 경우들에 유용함

    ✔예시

const mp = new Map() 
mp.set('a', 1)
mp.set('b', 2)
mp.set('cccc', 3) 
const st = new Set() 
st.add(1)
st.add(2)
st.add(3) 
const a = []
for(let i = 0; i < 10; i++)a.push(i)
for(let aa of a) console.log(aa)
for(let a of mp) console.log(a)
for(let a of st) console.log(a) 
/* 
a, b, c 
[ 'a', 1 ]
[ 'b', 2 ]
[ 'c', 3 ]
1
2
3
*/

참고

https://github.com/wnghdcjfe/csnote
https://www.zerocho.com/category/JavaScript/post/57df7063dfe17f0015e44121

profile
https://github.com/Foccy https://foccy-github-5er7huis0-foccy.vercel.app/?category=category1

0개의 댓글