[Spring] @Builder (Builder Patter)

2021. 10. 11. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ @Builder

 

์ด๊ฒƒ์„ Builder Pattern (๋นŒ๋” ํŒจํ„ด)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์šฉํ•œ ๊ฒƒ์ธ๋ฐ, ์•„๋ž˜ ์žฅ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ ๊ฒƒ์ด์—์š”

  1. ํ•„์š”ํ•œ Data๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  2. ์œ ์—ฐ์„ฑ์„ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ์Œ
  3. ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ
  4. ๋ถˆ๋ณ€์„ฑ ํ™•๋ณด ๊ฐ€๋Šฅ

 

1. ํ•„์š”ํ•œ Data๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ๋ฅผ ๋“ค์–ด Member ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š”๋ฐ, id๋ผ๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ๋ฐ‘์—์„œ ์ ๊ฒ ์ง€๋งŒ, @GeneratedValue๋ฅผ ํ†ตํ•ด Mariadb์— Auto Increament๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์šฉ์ž๊ฐ€ ์ง์ ‘ ๊ฑด๋“ค ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ์ด์—์š”. ์ด๋ ‡๊ฒŒ ๋งค๊ฐœ๋ณ„์ˆ˜๊ฐ€ ํ•„์š”์—†๋Š” ์ƒํ™ฉ์ด๋ผ ํ•  ๋•Œ, Builder Pattern์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด id์— ๋”๋ฏธ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ฑฐ๋‚˜, id๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”. ์ด๋Ÿฌํ•œ ์ž‘์—…์ด ์—„~~~~~~~~~~~~์ฒญ ๋งŽ๋‹ค๋ฉด ๋„ˆ๋ฌด๋„ˆ๋ฌด ํž˜๋“ค๊ฒƒ์ด์—์š”. ํ•˜์ง€๋งŒ ๋นŒ๋”๋ฅผ ์ด์šฉํ•˜๋ฉด ๋™์ ์œผ๋กœ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

Member member = Member.builder()
	.title("ํ…Œ์ŠคํŠธ")
    .content("์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!")
	.build();

 

์ด๋ ‡๊ฒŒ ํ•„์š”ํ•œ Data๋งŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” Builder์— ์žฅ์ ์€ Test์šฉ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ์šฉ์ดํ•˜๊ณ , ๋ถˆํ•„์š”ํ•œ Code์˜ ์–‘์„ ์ค„์ด๋Š” ์ด์ ์ด ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

2. ์œ ์—ฐ์„ฑ์„ ํ™•๋ณดํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ๋ฅผ ๋“ค์–ด Member Class์— ๊ฒฐํ˜ผ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜ Marriage๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค๊ณ  ์น˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑ์ž๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” Code๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ • ํ•ด ๋ณผ๊ฒŒ์š”!

 

// ์ด์ „
Member member = new Member("ํ…Œ์ŠคํŠธ", "์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!", "์ฃผ๋‹ˆํ•˜๋ž‘");

// ์ดํ›„
Member member = new Member("ํ…Œ์ŠคํŠธ", "์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!", "์ฃผ๋‹ˆํ•˜๋ž‘","๋ฏธํ˜ผ");
๋ฐ˜์‘ํ˜•

 

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋˜๋Š” ๋ณ€์ˆ˜ ํ•˜๋‚˜ ๋•Œ๋ฌธ์— ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ์žก์Šค๋Ÿฌ์šด ์ƒํ™ฉ์ด ์ƒ๊ธฐ๋Š” ๊ฒƒ์ด์—์š”.

๊ธฐ์กด ์ฝ”๋“œ์˜ ์–‘์ด ๋ฐฉ๋Œ€ํ•˜๋‹ค๋ฉด ๊ฐ๋‹นํ•˜๊ธฐ ์–ด๋ ต์ง€๋งŒ, ๋นŒ๋” ํŒจํ„ด์„ ์ด์šฉํ•˜๋ฉด ๋ณ€์ˆ˜๊ฐ€ ์ถ”๊ฐ€๋˜๋”๋ผ๋„ ๊ธฐ์กด ์ฝ”๋“œ์˜ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ์ฆ‰, ์œ ์—ฐํ•˜๊ฒŒ ๊ฐ์ฒด์˜ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

 

3. ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์•„์ ธ๋„ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.
์ƒ์„ฑ์ž๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์ฝ”๋“œ ๋ฆฌ๋”ฉ์ด ๊ธ‰๊ฒฉํ•˜๊ฒŒ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์ด์—์š”.

 

// ์ด์ „
Member member = new Member("ํ…Œ์ŠคํŠธ", "์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!", "์ฃผ๋‹ˆํ•˜๋ž‘");

// ์ดํ›„
Member member = new Member("ํ…Œ์ŠคํŠธ", "์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!", "์ฃผ๋‹ˆํ•˜๋ž‘","๋ฏธํ˜ผ");

 

์œ„ ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ๋ณด๋ฉด ๋„๋Œ€์ฒด๊ฐ€ ํ…Œ์ŠคํŠธ๊ฐ€ ๋ญ๊ณ , ์™œ ๊ฐ‘์ž๊ธฐ ์ž๊ธฐ์†Œ๊ฐœ๋ฅผ ํ•˜๋Š”์ง€๋ฅผ ์•Œ ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒƒ์ด์—์š”. ํด๋ž˜์Šค ๋ณ€์ˆ˜๊ฐ€ 4๊ฐœ ์ด์ƒ๋งŒ ๋˜๋„ ์ด๋ ‡๊ฒŒ ์•Œ์•„์ฐจ๋ฆฌ๊ธฐ๊ฐ€ ํž˜๋“  ๊ฒƒ์ด์ฃ . ํ•˜์ง€๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ์ด ๋œ๋‹ค๋ฉด ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์ด์—์š”.

 

Member member = Member.builder()
	.title("ํ…Œ์ŠคํŠธ")
    .contents("์•ˆ๋…•ํ•˜์„ธ์š”! ์ฃผ๋‹ˆํ•˜๋ž‘์ด์—์š”!")
    .writer("์ฃผ๋‹ˆํ•˜๋ž‘")
    .marriage("๋ฏธํ˜ผ")
    .build();

 

4. ๋ถˆ๋ณ€์„ฑ ํ™•๋ณด ๊ฐ€๋Šฅ

ํด๋ž˜์Šค ๋ณ€์ˆ˜์— final์„ ์„ ์–ธํ•˜๊ณ , ๊ฐ์ฒด์˜ ์ƒ์„ฑ์€ ๋นŒ๋”์—๊ฒŒ ๋งก๊ธฐ๋Š” ๊ฒƒ์ด ์ข‹์€ ๊ฒƒ์ด์—์š”.

๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ํ˜‘์—… ํ•˜๊ฑฐ๋‚˜, ๋ณ€๊ฒฝ์— ๋Œ€ํ•œ ์š”๊ตฌ ์‚ฌํ•ญ์ด ๋งŽ์€ ๊ฒฝ์šฐ ๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋ฅผ ๋”์šฑ ํ•„์š”๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”. ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์€ ๊ฒƒ์ด์—์š”. ์˜ˆ์™ธ์ ์ธ ์ƒํ™ฉ์€ Entity ๊ฐ์ฒด ๋˜๋Š” ๋„๋ฉ”์ธ ๊ฐ์ฒด๋กœ ๋ถ€ํ„ฐ DTO๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ์ง์ ‘ ๋นŒ๋”๋ฅผ ๋งŒ๋“ค๊ณ  ํ•˜๋Š” ์ž‘์—…์ด ๋ฒˆ๊ฑฐ๋กญ๊ธฐ ๋•Œ๋ฌธ์— Model Mapper์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ์„ ์œ„์ž„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

 


 

 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ช…๋ก์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•