2 minute read

Mention : Dependency Injection?๐Ÿ’‰ ์˜์กด์„ฑ ์ฃผ์ž…์ด๋ž€?

์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…(Dependency Injection)

โ€œA๊ฐ€ B๋ฅผ ์˜์กดํ•œ๋‹ค.โ€

์˜์กด๋Œ€์ƒ B๊ฐ€ ๋ณ€ํ•˜๋ฉด, ๊ทธ๊ฒƒ์ด A์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. -ํ† ๋น„์˜ ์Šคํ”„๋ง-

  • ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž
class Pizza {
	private PizzaRecipe pizzaRecipe;

	public Pizza() {
		PizzaRecipe = new PizzaRecipe();
	}
}
  • ํ”ผ์ž๋Š” ํ”ผ์ž ๋ ˆ์‹œํ”ผ์— ์˜์กดํ•œ๋‹ค.
    • ํ”ผ์ž ๋ ˆ์‹œํ”ผ๊ฐ€ ๋ณ€ํ™”ํ•˜๊ฒŒ ๋˜๋ฉด, ๋ณ€ํ™”๋œ ๋ ˆ์‹œํ”ผ์— ๋”ฐ๋ผ ํ”ผ์ž ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Š” ๋ ˆ์‹œํ”ผ์˜ ๋ณ€ํ™”๊ฐ€ ํ”ผ์ž์— ์˜ํ–ฅ์„ ๋ฏธ์ณค๊ธฐ ๋•Œ๋ฌธ์—, โ€œํ”ผ์ž๋Š” ํ”ผ์ž ๋ ˆ์‹œํ”ผ์— ์˜์กดํ•œ๋‹คโ€ ๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜์กด๊ด€๊ณ„๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•˜๊ธฐ

  • ๋” ๋‹ค์–‘ํ•œ PizzaRecipe๋ฅผ ์˜์กด ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•ด์•ผ ํ•œ๋‹ค.

      class Pizza {
      		private PizzaRecipe pizzaRecipe;
        		
      		public Pizza() {
      			pizzaRecipe = new PeperoniPizzaRecipe();
      			//pizzaRecipe = new PeperoniPizzaRecipe();
      			//pizzaRecipe = new CheesePizzaRecipe();
      		}
      }
        	
      interface PizzaRecipe {
      		newPizza();
      	}
      }
        
      class PeperoniPizzaRecipe implements PizzaRecipe {
      	public Pizza newPizza() {
      		return new PeperoniPizza();
      	}
      }
    
    • ์˜์กด๊ด€๊ณ„๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ƒํ™”ํ•˜๊ฒŒ ๋˜๋ฉด, ๋” ๋‹ค์–‘ํ•œ ์˜์กด ๊ด€๊ณ„๋ฅผ ๋งบ์„์ˆ˜๊ฐ€ ์žˆ๊ณ , ์‹ค์ œ ๊ตฌํ˜„ ํด๋ž˜์Šค์™€์˜ ๊ด€๊ณ„๊ฐ€ ๋Š์Šจํ•ด์ง€๊ณ , ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง„๋‹ค.

Dependency Injection?

  • ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๊ฒฐ์ •ํ•˜๊ณ  ์ฃผ์ž…ํ•˜๋Š” ๊ฒƒ์ด DI ์ด๋‹ค.

โ€œํด๋ž˜์Šค ๋ชจ๋ธ์ด๋‚˜ ์ฝ”๋“œ์—๋Š” ๋Ÿฐํƒ€์ž„ ์‹œ์ ์˜ ์˜์กด๊ด€๊ณ„๊ฐ€ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์˜์กดํ•˜๊ณ  ์žˆ์–ด์•ผํ•œ๋‹ค.โ€

โ€๋Ÿฐํƒ€์ž„ ์‹œ์ ์˜ ์˜์กด๊ด€๊ณ„๋Š” ์ปจํ…Œ์ด๋„ˆ๋‚˜ ํŒฉํ† ๋ฆฌ ๊ฐ™์€ ์ œ3์˜ ์กด์žฌ๊ฐ€ ๊ฒฐ์ •ํ•œ๋‹ค.โ€

โ€์˜์กด๊ด€๊ณ„๋Š” ์‚ฌ์šฉํ•  ์˜ค๋ธŒ์ ํŠธ์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•ด์คŒ์œผ๋กœ์จ ๋งŒ๋“ค์–ด์ง„๋‹ค.โ€ -ํ† ๋น„์˜ ์Šคํ”„๋ง-

  • DI๋Š” ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ํด๋ž˜์Šค ๋ณ€์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค์ด ๊ณง DI๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋Ÿฐํƒ€์ž„ ์‹œ์ ์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•˜์—ฌ DI๊ตฌํ˜„์ด ์™„์„ฑ๋œ๋‹ค.
  • ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
```java
class Pizza {
	private PizzaRecipe pizzaRecipe;

	public Pizza(PizzaRecipe pizzaRecipe) {
		this.pizzaRecipe = pizzaRecipe;
	}
}

class PizzaShopOwner {
	private Pizza pizza = new Pizza(new PeperoniRecipe());

	public void changeMenu() {
		pizza = new Pizza(new CheesePizzaRecipe());
	}
}
```
  • ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉ

      class Pizza {
      	private PizzaRecipe pizzaRecipe = new PeperoniRecipe();
        
      	public void setPizzaRecipe(PizzaRecipe pizzaRecipe) {
      		this.pizzaRecipe = pizzaRecipe;
      	}
      }
        
      class PizzaShopOwner {
      	private Pizza pizza = new Pizza();
        
      	public void changeMenu() {
      		pizza.setPizzaRecipe(new CheesePizzaRecipe());
      	}
      }
    

DI์˜ ์žฅ์ 

  • ์˜์กด์„ฑ์ด ์ค„์–ด๋“ ๋‹ค.
    • DI๋กœ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜์—ˆ์„ ๋•Œ, ์ฃผ์ž… ๋ฐ›๋Š” ๋Œ€์ƒ์ด ๋ณ€ํ•˜๋”๋ผ๋„ ๊ทธ ๊ตฌํ˜„ ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•  ์ผ์ด ์—†๊ฑฐ๋‚˜ ์ค„์–ด๋“ค๊ฒŒ๋จ
  • ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’์€ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค.
    • ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
  • ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค.
    • ๋ถ„๋ฆฌํ•ด์„œ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ใ…‡์žˆ๋‹ค.
  • ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง„๋‹ค.
    • ๊ธฐ๋Šฅ๋“ค์„ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•˜๊ฒŒ ๋˜์–ด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง„๋‹ค.
  • Summary
    • DI(Dependency Injection) ๋ž€ ์Šคํ”„๋ง์ด ๋‹ค๋ฅธ ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์ฐจ๋ณ„ํ™”๋˜์–ด ์ œ๊ณตํ•˜๋Š” ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž… ๊ธฐ๋Šฅ์œผ๋กœ, ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•œ ํ›„ ์ฃผ์ž… ์‹œ์ผœ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค. DI(์˜์กด์„ฑ ์ฃผ์ž…)๋ฅผ ํ†ตํ•ด์„œ ๋ชจ๋“ˆ ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง€๊ณ  ์œ ์—ฐ์„ฑ์ด ๋†’์•„์ง„๋‹ค. IoC(Inversion of Control) ๋ž€ โ€œ์ œ์–ด์˜ ์—ญ์ „โ€ ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ, ๋ง ๊ทธ๋Œ€๋กœ ๋ฉ”์†Œ๋“œ๋‚˜ ๊ฐ์ฒด์˜ ํ˜ธ์ถœ์ž‘์—…์„ ๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์™ธ๋ถ€์—์„œ ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. IoC๋Š” ์ œ์–ด์˜ ์—ญ์ „์ด๋ผ๊ณ  ๋งํ•˜๋ฉฐ, ๊ฐ„๋‹จํžˆ ๋งํ•ด โ€œ์ œ์–ด์˜ ํ๋ฆ„์„ ๋ฐ”๊พผ๋‹คโ€๋ผ๊ณ  ํ•œ๋‹ค. ๊ฐ์ฒด์˜ ์˜์กด์„ฑ์„ ์—ญ์ „์‹œ์ผœ ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ์ค„์ด๊ณ  ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์—ฌ ๊ฐ€๋…์„ฑ ๋ฐ ์ฝ”๋“œ ์ค‘๋ณต, ์œ ์ง€ ๋ณด์ˆ˜๋ฅผ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

Reference ๐Ÿ“š

https://tecoble.techcourse.co.kr/post/2021-04-27-dependency-injection/

Leave a comment