Arthur's Blog

Service ๊ตฌํ˜„์ฒด๋ฅผ ์ถ”์ƒํ™” ํ•˜๋Š”๊ฒƒ์— ๋Œ€ํ•ด.. ๋ณธ๋ฌธ

Backend/Spring Boot

Service ๊ตฌํ˜„์ฒด๋ฅผ ์ถ”์ƒํ™” ํ•˜๋Š”๊ฒƒ์— ๋Œ€ํ•ด..

Leeseojune53 2023. 7. 1. 00:26

๐ŸŽŠ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—..

์ดˆ๋ณด ๊ฐœ๋ฐœ์ž์ธ ์ €์˜ ์˜๊ฒฌ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

interface-๊ตฌํ˜„์ฒด๊ตฌ์กฐ๋ฅผ ํ”ํžˆ ๋ฌด์ง€์„ฑ, ์ฆ‰ ์ด์œ ์—†์ด ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๊ด€๋ก€๋ผ์„œ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋ก€์„ ๋งŽ์ด ๋ด์™”์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๊ทธ๊ฒƒ์„ ๊ตณ์ด interface๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”๊ฐ€? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ๊ธ€์„ ์ ์—ˆ์Šต๋‹ˆ๋‹ค.

โœจ ๋‹คํ˜•์„ฑ

Service์˜ ๊ตฌํ˜„์ฒด๋ฅผ interface๋กœ ์ถ”์ƒํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ์ค‘ ๊ฐ€์žฅ ํฐ ์ด์œ ๊ฐ€ ๋‹คํ˜•์„ฑ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹คํ˜•์„ฑ์ด๋ž€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•˜๋‚˜์˜ Type์— ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ์„ฑ์งˆ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์ธ ์Šคํ”„๋ง์—์„œ ์„œ๋น„์Šค์˜ ๋‹คํ˜•์„ฑ์„ ๊ตณ์ด ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋ฅผ ์ €๋Š” ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, UserService๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ๊ธฐ๋Šฅ์œผ๋กœ ์œ ์ € ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ, ๋‹‰๋„ค์ž„ ๋ณ€๊ฒฝ ๋“ฑ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ์‹œ๋‹ค. ์ด UserService์˜ ๊ตฌํ˜„์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ๊ธธ์ƒํ™ฉ์ด ์•„๋‹Œ๋ฐ๋„ ์ด์œ ์—†์ด interface-๊ตฌํ˜„์ฒด๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์˜ˆ์‹œ๋กœ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ €๋Š” interface๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , ์„œ๋น„์Šค๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ ๋„๋ฉ”์ธ๋ณ„ ์„œ๋น„์Šค ๋ถ„๋ฆฌ๋ฅผ ๋” ์ง‘์ค‘ํ•ด์„œ ํ•˜๋‚˜์˜ ๋„๋ฉ”์ธ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์„œ๋น„์Šค๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๊ฒŒ ์„ค๊ณ„๋ฅผ ํ•˜๋Š”๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ ์ •๋ฆฌ

๋”ฐ๋ผ์„œ ์ €๋Š” Service์˜ interface๋ฅผ ๋‘์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ์„ ํ•˜๋‹ค๊ฐ€, ์œ ์—ฐํ•จ๊ณผ ํ™•์žฅ์„ฑ์ด ํ•„์š”ํ•  ๋•Œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ์กฐ๋ฅผ ์ •ํ•˜๋Š”๋ฐ ์ •๋‹ต์€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋งํ•œ ๋ฐฉ๋ฒ•๋˜ํ•œ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋งž๋Š” ๊ตฌ์กฐ์ผ์ˆ˜๋„, ์ž˜๋ชป๋œ ๊ตฌ์กฐ์ผ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์ €์™€ ๋‹ค๋ฅธ ์ƒ๊ฐ์„ ๊ฐ€์ง€์‹ ๋ถ„๋“ค์˜ ์˜๊ฒฌ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹คโค

๋ถ€์กฑํ•œ ๊ธ€์ด์ง€๋งŒ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜๐Ÿ˜