728x90

πŸ›  Builder Pattern μ΄λž€

객체의 생성 단계듀을 μΊ‘μŠν™”ν•˜μ—¬ 객체의 생성을 μœ μ—°ν•˜κ²Œ ν•΄μ£ΌλŠ” νŒ¨ν„΄μž…λ‹ˆλ‹€. κ·Έλž˜μ„œ λΉŒλ” νŒ¨ν„΄μ€ μƒμ„±μžμ— λ“€μ–΄κ°ˆ 맀개 λ³€μˆ˜κ°€ λ§Žλ“  적든 μ°¨λ‘€λŒ€λ‘œ 맀개 λ³€μˆ˜λ₯Ό 받아듀이고 λͺ¨λ“  맀개 λ³€μˆ˜λ₯Ό 받은 뒀에  μ΄ λ³€μˆ˜λ“€μ„ ν†΅ν•©ν•΄μ„œ ν•œ λ²ˆμ— μ‚¬μš©ν•©λ‹ˆλ‹€.

 

πŸ›  Builder Pattern

Builder Pattern의 μž₯점에 λŒ€ν•œ μ˜ˆμ‹œλ₯Ό μ•Œμ•„λ³΄μž. Builder Pattern의 μž₯점은 λ‹€μŒκ³Ό 같은 것듀이 μžˆλ‹€.

  • 가독성을 높일 수 μžˆλ‹€.
  • λΆˆλ³€μ„±μ„ 확보할 수 μžˆλ‹€.
  • ν•„μš”ν•œ λ°μ΄ν„°λ§Œ 섀정이 κ°€λŠ₯ν•˜λ‹€.

이제 μ˜ˆμ‹œλ₯Ό μ°¨κ·Όμ°¨κ·Ό 보자

 

πŸ› μ²«λ²ˆμ§Έλ‘œ μ–΄λ–€ μΈ‘λ©΄μ—μ„œ 가독성을 λ†’μ—¬μ£ΌλŠ”μ§€ μ•Œμ•„λ³΄μž.

public class User {

    private Long id;
    private String email;
    private String name;
    private String password;
    private int age;

    public User(Long id, String email, String name, String password, int age) {
        this.id = id;
        this.email = email;
        this.name = name;
        this.password = password;
        this.age = age;
    }
}

μš°μ„  기본적인 μƒμ„±μžλ‘œ λ„˜κΈ°λŠ” 방법이닀. 

User user = new User(1L,"rrr11@naver.com","KK","l12313123",14);

 

λͺ¨λ“  데이터λ₯Ό μƒμ„±μžλ‘œ λ„˜κΈ°λ©΄ μœ„μ²˜λŸΌ μž‘μ„±ν•˜λ‹€ 보면 μΈμžκ°€ λ§Žμ•„μ§€λ©΄  μˆœμ„œλ₯Ό λ°”κΏ” 넣을 μˆ˜λ„ 있고 μ§κ΄€μ μœΌλ‘œ ν•œλˆˆμ— μ•Œμ•„λ³΄κΈ° μ–΄λ ΅λ‹€λŠ” 단점이 μ‘΄μž¬ν•œλ‹€. 이제 이 문제λ₯Ό 점차 ν•΄κ²°ν•΄λ³΄μž.

 

  @Builder
    public User(Long id, String email, String name, String password, int age) {
        this.id = id;
        this.email = email;
        this.name = name;
        this.password = password;
        this.age = age;
    }

 

μƒμ„±μžλ₯Ό μœ„μ²˜λŸΌ λΉŒλ” νŒ¨ν„΄μœΌλ‘œ λ„˜κΈ°λ©΄ 값을 μ„€μ •ν•  λ•Œ 

	User user = User.builder()
                .id(1L)
                .email("rrr11@naver.com")
                .name("KK")
                .password("l12313123")
                .age(14)
                .build();

 

μ΄λŸ°μ‹μœΌλ‘œ ν•œλˆˆμ— μ§κ΄€μ μœΌλ‘œ 보이게 값을 μ‚½μž…ν•  수 μžˆλ‹€.


πŸ› λ‘λ²ˆμ§Έλ‘œ λΆˆλ³€μ„±μ„ ν™•λ³΄ν•˜λŠ” μ˜ˆμ‹œλ₯Ό 보자 

 

μžλ°” 빈 νŒ¨ν„΄μœΌλ‘œ @Setter둜 값을 μ‚½μž…ν•  수 μžˆλŠ”λ° 이 방식은 가독성도 μƒμ„±μž νŒ¨ν„΄λ³΄λ‹€ μ’‹μ•„μ§€κ³  객체λ₯Ό μƒμ„±ν•˜κΈ°μ—λ„ νŽΈν•΄μ‘Œμ§€λ§Œ, ν•¨μˆ˜ 호좜 ν•œ 번으둜 객체λ₯Ό 생성할 수 μ—†κ³   κ°μ²΄ 일관성이 μΌμ‹œμ μœΌλ‘œ 깨질 수 μžˆλ‹€.

 

@Setter
public class User {

    private Long id;
    private String email;
    private String name;
    private String password;
    private int age;
    
  }
  
  
   User user = new User();
        user.setId(1L);
        user.setEmail("rrr11@naver.com");
        user.setName("KK");
        user.setPassword("l12313123");
        user.setAge(23);

μœ„μ²˜λŸΌ @Setterλ₯Ό μ—΄μ–΄λ‘λŠ” 것은 λΆˆν•„μš”ν•˜κ²Œ ν™•μž₯ κ°€λŠ₯성을 μ—΄μ–΄λ‘λŠ” 것이기 λ•Œλ¬Έμ— Open-Close 법칙에도 μœ„λ°°κ°€ 되고, λΆˆν•„μš”ν•œ μ½”λ“œ 리딩을 μœ λ°œν•œλ‹€.

 


πŸ› μ„Έλ²ˆμ§Έλ‘œ ν•„μš”ν•œ 데이터λ₯Ό μ„€μ •ν•˜λŠ” μ˜ˆμ‹œλ₯Ό 보자.

 

μš°μ„  데이터λ₯Ό 전솑할 λ•Œ κ·Έ μž‘μ—…μ— ν•„μš”ν•œ λ°μ΄ν„°λ§Œ λ„˜κΈΈ 수 μžˆλ‹€. ν•„μš”ν•œ λ°μ΄ν„°λ§Œ λ„˜κΈ°κ²Œ 되면 μœ μ € μ •λ³΄μ˜ νŒ¨μŠ€μ›Œλ“œ 등을 λ…ΈμΆœν•˜μ§€ μ•Šμ„ 수 있기 λ•Œλ¬Έμ— λ³΄μ•ˆμ μœΌλ‘œ 이점을 κ°€μ§„λ‹€.

일단 νŒ¨μŠ€μ›Œλ“œμ™€ μ•„μ΄λ””λŠ” λ„˜κΈ°λ©΄ μ•ˆλœλ‹€κ³  가정을 ν•΄λ³΄μž.

기본적인 μƒμ„±μžλ‘œ λ„˜κΈ°λ©΄  μš°μ„  λͺ¨λ“  데이터가 λ„˜μ–΄κ°„λ‹€. 이 문제λ₯Ό μƒμ„±μžλ‘œ ν•΄κ²°ν•΄λ³΄μž.

public class User {

    private Long id;
    private String email;
    private String name;
    private String password;
    private int age;

    public User(String email, String name, int age) {
        this.email = email;
        this.name = name;
        this.age = age;
    }

    public User(Long id, String password) {
        this.id = id;
        this.password = password;
    }
}

μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ λΆ„λ¦¬ν•΄μ„œ ν•˜λ©΄ 아이디와 νŒ¨μŠ€μ›Œλ“œλ₯Ό λΉΌκ³  λ„˜κΈΈ 수 μžˆλ‹€. 이런 μ‹μœΌλ‘œ λΆ„λ¦¬ν•˜λ©΄ μ½”λ“œκ°€ μ§€μ €λΆ„ ν•΄μ§€κ³  가독성이 λ–¨μ–΄μ§„λ‹€.

λΉŒλ” νŒ¨ν„΄μ„ μ‚¬μš©ν•΄λ³΄μž 

    @Builder
    public User(Long id, String email, String name, String password, int age) {
        this.id = id;
        this.email = email;
        this.name = name;
        this.password = password;
        this.age = age;
    }
	User user = User.builder()
                .email("rrr11@naver.com")
                .name("KK")
                .age(14)
                .build();

 

μ΄λŸ°μ‹μœΌλ‘œ ν•„μš”ν•œ λ°μ΄ν„°λ§Œ μ„€μ •ν•  수 μžˆλ‹€. κ·ΈλŸ¬λ―€λ‘œ λΆˆν•„μš”ν•œ μ½”λ“œλ₯Ό 쀄여쀀닀. μ΄λŸ¬ν•œ λ°©λ²•μœΌλ‘œ μž‘μ—…λ§ˆλ‹€ ν•„μš”ν•œ 데이터λ₯Ό 엔티티객체 λ˜λŠ” 도메인 κ°μ²΄λ‘œλΆ€ν„° DTO둜 λ§Œλ“€μ–΄ λ„˜κ²¨μ€„ 수 μžˆλ‹€. 

 

 

DTO μ˜ˆμ‹œ

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
public class UserDto {
    
    private String email;
    private String name;
    private int age;
}

μ΄λŸ°μ‹μœΌλ‘œ DTOλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.


κ·ΈλŸ¬λ‚˜ λΉŒλ” νŒ¨ν„΄μ— μž₯점만 μ‘΄μž¬ν•˜λŠ” 것이 μ•„λ‹ˆλ‹€. 

μš°μ„  객체λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ λΉŒλ” 객체λ₯Ό 생성해야 ν•˜κ³ , λ‹€λ₯Έ νŒ¨ν„΄λ³΄λ‹€ λ§Žμ€ μ½”λ“œλ₯Ό μš”κ΅¬ν•˜κΈ° λ•Œλ¬Έμ— μΈμžκ°€ μΆ©λΆ„νžˆ λ§Žμ•„μ§„ μƒν™©μ—μ„œ μ΄μš©ν•  ν•„μš”κ°€ μžˆλ‹€.

 

GITHUB μ†ŒμŠ€μ½”λ“œ : https://github.com/ryudongjae/blog-ex

 

GitHub - ryudongjae/blog-ex

Contribute to ryudongjae/blog-ex development by creating an account on GitHub.

github.com


REPERENCE

728x90