λΉŒλ” νŒ¨ν„΄ (Builder Pattern)

πŸ”” λΉŒλ” νŒ¨ν„΄μ΄λž€? πŸ”” κΈ°μ‘΄ μƒμ„±μž λ°©μ‹κ³Όμ˜ 차이점 What? Why? λΉŒλ” νŒ¨ν„΄(Builder Pattern)은 λ³΅μž‘ν•œ 객체λ₯Ό λ‹¨κ³„λ³„λ‘œ 생성할 수 있게 ν•΄μ£ΌλŠ” λ””μžμΈ νŒ¨ν„΄μ΄λ‹€. 객체의 생성 과정을 μΆ”μƒν™”ν•˜μ—¬, λ‹€μ–‘ν•œ ν˜•νƒœμ˜ 객체λ₯Ό μœ μ—°ν•˜κ²Œ 생성할 수 μžˆλ„λ‘ 도와쀀닀. κΈ°μ‘΄ μƒμ„±μž λ°©μ‹κ³Όμ˜ 비ꡐ κΈ°μ‘΄ μƒμ„±μž 방식 기쑴의 μƒμ„±μž 방식은 객체λ₯Ό 생성할 λ•Œ ν•„μš”ν•œ λͺ¨λ“  νŒŒλΌλ―Έν„°λ₯Ό μƒμ„±μžμ— 전달해야 ν•œλ‹€. public class User { private Long id; private String userId; private String username; private Set<Role> roles; // 기타 ν•„λ“œ 및 λ©”μ„œλ“œ public User(Long id, String userId, String username, Set<Role> roles) { this.id = id; this.userId = userId; this.username = username; this.roles = roles; } } λΉŒλ” νŒ¨ν„΄ 방식 λΉŒλ” νŒ¨ν„΄μ„ μ‚¬μš©ν•˜λ©΄ 객체의 ν•„λ“œλ₯Ό λ‹¨κ³„λ³„λ‘œ μ„€μ •ν•  수 있으며, 가독성이 λ†’μ•„μ§€κ³  선택적인 ν•„λ“œλ₯Ό μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆλ‹€. ...

December 19, 2024 13:02 Β· 2 min Β· 354 words Β· nakji