๊ด€๋ฆฌ ๋ฉ”๋‰ด

โœ๐Ÿป๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ

@Builder๋ฅผ ํ†ตํ•ด ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ ๋ณธ๋ฌธ

ํ”„๋กœ์ ํŠธ/Heeverse

@Builder๋ฅผ ํ†ตํ•ด ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ธฐ

์ฉ์‹œํ‚ด 2023. 7. 21. 15:37
728x90

๋ฌธ์ œ์ƒํ™ฉ

member ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ํŽธํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด lombok์˜ @Builder์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

member ํด๋ž˜์Šค์— ํ•„์š”ํ•œ ํ•„๋“œ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ƒ์„ฑ์ž ์ƒ์„ฑ ํ›„ @Builder์„ ๋ถ™์—ฌ builder ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ํ•˜์˜€๋‹ค.

@Builder
public Member(String id, String password, String userName, String email) {
    this.id = id;
    this.password = password;
    this.userName = userName;
    this.email = email;
}

ํ•˜์ง€๋งŒ ํ•„๋“œ์— null์ด ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ๊ฐ€ ๊ณ ๋ ค๋˜์ง€ ์•Š์•˜๋‹ค.

 

@Builder์— ์˜ํ•ด ์ปดํŒŒ์ผ ์‹œ ์ƒ์„ฑ๋œ ์–ด๋…ธํ…Œ์ด์…˜ ํ”„๋กœ์„ธ์‹ฑ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณด์ž

public static class MemberBuilder {
    private String id;
    private String password;
    private String userName;
    private String email;

    MemberBuilder() {
    }

    public MemberBuilder id(final String id) {
        this.id = id;
        return this;
    }

    public MemberBuilder password(final String password) {
        this.password = password;
        return this;
    }

    public MemberBuilder userName(final String userName) {
        this.userName = userName;
        return this;
    }

    public MemberBuilder email(final String email) {
        this.email = email;
        return this;
    }

    public Member build() {
        return new Member(this.id, this.password, this.userName, this.email);
    }

    public String toString() {
        return "Member.MemberBuilder(id=" + this.id + ", password=" + this.password + ", userName=" + this.userName + ", email=" + this.email + ")";
    }
}

@Builder์œผ๋กœ๋Š” ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋Œ€ํ•ด final๋กœ ์„ ์–ธ๋˜์–ด ์žฌํ• ๋‹น์—๋Š” ์•ˆ์ „ํ•˜์ง€๋งŒ, null์— ๋Œ€ํ•œ ์ฒดํฌ๋Š” ์—†๊ธฐ์— ๊ฐœ๋ฐœ์ž๊ฐ€ ์ถ”๊ฐ€์ ์ธ null ์ฒดํฌ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

  1. ํ•„์ˆ˜๋กœ ๊ฐ’์ด ์„ธํŒ…๋˜์–ด์•ผ ํ•˜๋Š” ํ•„๋“œ๋Š” builder ์ƒ์„ฑ์ž ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ž์— @NotNull์„ ๋ถ™์ธ๋‹ค.
@Builder
public Member(@NotNull String id, @NotNull String password, String userName, String email) {
    this.id = id;
    this.password = password;
    this.userName = userName;
    this.email = email;
}

 

2. Assert ๊ตฌ๋ฌธ์œผ๋กœ null์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ๋ฅผ ํ•œ๋‹ค.

@Builder
public Member(String id, String password, String userName, String email) {
    Assert.notNull(password,"id๋Š” ํ•„์ˆ˜๊ฐ’ ์ž…๋‹ˆ๋‹ค.");
    Assert.notNull(password,"password๋Š” ํ•„์ˆ˜๊ฐ’ ์ž…๋‹ˆ๋‹ค.");

    this.id = id;
    this.password = password;
    this.userName = userName;
    this.email = email;
}

Assert

Spring์—์„œ ์ œ๊ณตํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์œ ํ‹ธ ํด๋ž˜์Šค๋กœ, runtime์‹œ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์—๋Ÿฌ๋ฅผ ์ผ์ฐ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค€๋‹ค. Assert ๊ตฌ๋ฌธ์— ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ exception์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

notNull(object)๋Š” ์Šคํ”„๋ง 6.1 ๋ฒ„์ „์—์„œ ์ œ๊ฑฐ ์˜ˆ์ •์ด๋ฏ€๋กœ notNull(object, message) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

์ฝ”๋“œ์˜ ๊ธธ์ด๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•ด ์ฃผ๊ณ  ํ•„๋“œ๊ฐ€ ๋งŽ์•„๋„ ์•ˆ์ „ํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” @Builder ์ง€๋งŒ ํŽธํ•˜๊ฒŒ ์“ฐ๋Š” ๋งŒํผ ์•ˆ์ „์„ฑ์—๋„ ์œ ์˜ํ•˜๋ฉฐ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค.

 


[์ฐธ๊ณ ]

https://projectlombok.org/features/Builder

https://lemontia.tistory.com/913

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/Assert.html

728x90
๋ฐ˜์‘ํ˜•