[TIL] 230630 DI, IoC

CountryGirlยท2023๋…„ 6์›” 30์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
12/80

๐Ÿ“Œ DI (Dependency Injection)

: ์˜์กด์„ฑ ์ฃผ์ž…
๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•œ ํ›„ ์ฃผ์ž…์„ ์‹œ์ผœ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

  • ํ•„๋“œ ์ฃผ์ž…
public class Consumer {

    Food food;

    void eat() {
        this.food.eat();
    }

    public static void main(String[] args) {
        Consumer consumer = new Consumer();
        consumer.food = new Chicken();
        consumer.eat();

        consumer.food = new Pizza();
        consumer.eat();
    }
}

interface Food {
    void eat();
}

class Chicken implements Food{
    @Override
    public void eat() {
        System.out.println("์น˜ํ‚จ์„ ๋จน๋Š”๋‹ค.");
    }
}

class Pizza implements Food{
    @Override
    public void eat() {
        System.out.println("ํ”ผ์ž๋ฅผ ๋จน๋Š”๋‹ค.");
    }
}
  • ๋ฉ”์„œ๋“œ ์ฃผ์ž…
public class Consumer {

    Food food;

    void eat() {
        this.food.eat();
    }

    public void setFood(Food food) {
        this.food = food;
    }

    public static void main(String[] args) {
        Consumer consumer = new Consumer();
        consumer.setFood(new Chicken());
        consumer.eat();

        consumer.setFood(new Pizza());
        consumer.eat();
    }
}

interface Food {
    void eat();
}

class Chicken implements Food{
    @Override
    public void eat() {
        System.out.println("์น˜ํ‚จ์„ ๋จน๋Š”๋‹ค.");
    }
}

class Pizza implements Food{
    @Override
    public void eat() {
        System.out.println("ํ”ผ์ž๋ฅผ ๋จน๋Š”๋‹ค.");
    }
}
  • ์ƒ์„ฑ์ž ์ฃผ์ž…: ๐Ÿ‘๐Ÿป ์ถ”์ฒœ!! ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์„ฑ์„ ์ง€์ผœ์ฃผ๊ธฐ ์œ„ํ•ด์„œ !
public class Consumer {

    Food food;

    public Consumer(Food food) {
        this.food = food;
    }

    void eat() {
        this.food.eat();
    }

    public static void main(String[] args) {
        Consumer consumer = new Consumer(new Chicken());
        consumer.eat();

        consumer = new Consumer(new Pizza());
        consumer.eat();
    }
}

interface Food {
    void eat();
}

class Chicken implements Food{
    @Override
    public void eat() {
        System.out.println("์น˜ํ‚จ์„ ๋จน๋Š”๋‹ค.");
    }
}

class Pizza implements Food{
    @Override
    public void eat() {
        System.out.println("ํ”ผ์ž๋ฅผ ๋จน๋Š”๋‹ค.");
    }
}

๐Ÿ“Œ IoC (Inversion of Control)

: ์ œ์–ด์˜ ์—ญ์ „
Consumer๊ฐ€ ์ง์ ‘ food๋ฅผ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด Food๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„  ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๋‹ค.
์ œ์–ดํ๋ฆ„: Consumer โž” food

food โž” Consumer ๋กœ ์ œ์–ดํ๋ฆ„์„ ๋ฐ”๊พธ์–ด ๋งŽ์€ ์ฝ”๋“œ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

  1. ๊ฐ์ฒด ์ƒ์„ฑ
  2. ์˜์กด์„ฑ ๊ฐ์ฒด ์ƒ์„ฑ - ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑ
  3. ์˜์กด์„ฑ ๊ฐ์ฒด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

[IoC]
1. ๊ฐ์ฒด ์ƒ์„ฑ
2. ์˜์กด์„ฑ ๊ฐ์ฒด ์ฃผ์ž… - ์Šค์Šค๋กœ๊ฐ€ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ œ์–ด๊ถŒ์„ Spring์—์„œ ์œ„์ž„ํ•˜์—ฌ Spring์ด ๋งŒ๋“ค์–ด๋†“์€ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํžŒ๋‹ค.
3. ์˜์กด์„ฑ ๊ฐ์ฒด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

โœ… ์ฐธ๊ณ ์‚ฌ์ดํŠธ

https://velog.io/@gillog/Spring-DIDependency-Injection

profile
๐Ÿ’ป๐ŸŒพ์‹œ๊ณจ์†Œ๋…€์˜ ์—‰๋ง์ง•์ฐฝ ๊ฐœ๋ฐœ ์„ฑ์žฅ์ผ์ง€๐ŸŒพ๐Ÿ’ป (2023.05.23 ~)

0๊ฐœ์˜ ๋Œ“๊ธ€