๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/์„ธ์…˜

Java ์ถ”๊ฐ€ ๋ฏธ๋‹ˆ ๊ณผ์ œ_๋‹คํ˜•์„ฑ, DI ์—ฐ์Šต

by carrot0911 2024. 12. 2.

๋‹คํ˜•์„ฑ, DI ์—ฐ์Šต

 

๊ณผ์ œ ๊ฐœ์š” โœ๏ธ

  • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(OOP)์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŠน์ • ์ข…๋ฅ˜์˜ ๋™๋ฌผ๋งŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” MyHouse ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์„ค๊ณ„ํ•˜๊ณ , ํƒ€์ž… ์•ˆ์ „์„ฑ์„ ํ™•๋ณดํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค.

 

โญ์š”๊ตฌ์‚ฌํ•ญโญ

1. Animal ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„

  • ๋ชจ๋“  ๋™๋ฌผ์ด ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
void makeSound();   // ๋™๋ฌผ์ด ๋‚ด๋Š” ์†Œ๋ฆฌ๋ฅผ ์ถœ๋ ฅ
void eat(String food); // ํŠน์ • ์Œ์‹์„ ๋จน๋Š” ๋™์ž‘์„ ์ถœ๋ ฅ
boolean isHungry(); // ๋™๋ฌผ์ด ๋ฐฐ๊ณ ํ”ˆ ์ƒํƒœ์ธ์ง€ ๋ฐ˜ํ™˜

2. Dog์™€ Cat ํด๋ž˜์Šค ๊ตฌํ˜„

  • Animal ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • ๊ฐ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • makeSound : ๋™๋ฌผ์ด ๋‚ด๋Š” ์†Œ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
      • Dog : "Bark!"
      • Cat : "Meow!"
    • eat : ๋™๋ฌผ์ด ์Œ์‹์„ ๋จน๋Š” ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉฐ, ๋ฐฐ๊ณ ํ”” ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
      • ๋ฐฐ๊ณ ํ”„๋ฉด ์Œ์‹์„ ๋จน๊ณ  ๋ฐฐ๊ณ ํ”” ์ƒํƒœ๊ฐ€ false๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.
      • ๋ฐฐ๊ณ ํ”„์ง€ ์•Š์œผ๋ฉด ์Œ์‹์„ ๊ฑฐ๋ถ€ํ•˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    • isHungry : ๋™๋ฌผ์ด ๋ฐฐ๊ณ ํ”ˆ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

3. MyHouse ํด๋ž˜์Šค ๊ตฌํ˜„

  • ํŠน์ • ๋™๋ฌผ ํ•œ ์ข…๋ฅ˜๋งŒ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.
  • ๋‹ค์Œ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ์ƒ์„ฑ์ž : ํŠน์ • ๋™๋ฌผ ๊ฐ์ฒด๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • makeAnimalSound : ๊ด€๋ฆฌํ•˜๋Š” ๋™๋ฌผ์˜ ์†Œ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
    • feedAnimal : ๊ด€๋ฆฌํ•˜๋Š” ๋™๋ฌผ์—๊ฒŒ ์Œ์‹์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋™๋ฌผ์˜ ๋ฐฐ๊ณ ํ”” ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • isHungry : ๊ด€๋ฆฌํ•˜๋Š” ๋™๋ฌผ์ด ๋ฐฐ๊ณ ํ”ˆ ์ƒํƒœ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

4. Main ํด๋ž˜์Šค ๊ตฌํ˜„

  • Main ํด๋ž˜์Šค์—์„œ ๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • Dog์™€ Cat ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • MyHouse ๊ฐ์ฒด๋ฅผ ๊ฐ๊ฐ Dog์™€ Cat์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์•„๋ž˜ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐ ๋™๋ฌผ์˜ ์†Œ๋ฆฌ ๋‚ด๊ธฐ
    • ๊ฐ ๋™๋ฌผ์˜ ๋ฐฐ๊ณ ํ”ˆ์ง€ ์—ฌ๋ถ€ ํ™•์ธ
    • ๊ฐ ๋™๋ฌผ์—๊ฒŒ ์Œ์‹์„ ๋จน์ธ๋‹ค.
    • ๊ฐ ๋™๋ฌผ์—๊ฒŒ ์Œ์‹์„ ๋จน์ธ๋‹ค. (๋ฐ˜๋ณต)
    • ๊ฐ ๋™๋ฌผ์˜ ๋ฐฐ๊ณ ํ”ˆ์ง€ ์—ฌ๋ถ€ ํ™•์ธ

๊ตฌํ˜„ ๊ฒฐ๊ณผ ์˜ˆ์‹œ โœ๏ธ

Dog house:
Dog says: Bark! // ์†Œ๋ฆฌ ๋‚ด๊ธฐ
Dog is hungry.  // ๋ฐฐ๊ณ ํ”ˆ์ง€ ํ™•์ธ
Dog eats Bone.  // Bone ๋จน์ด๊ธฐ
Dog is not hungry. // Bone ๋จน์ด๊ธฐ
Dog is not hungry. // ๋ฐฐ๊ณ ํ”ˆ์ง€ ํ™•์ธ

Cat house:
Cat says: Meow! // ์†Œ๋ฆฌ ๋‚ด๊ธฐ
Cat is hungry.  // ๋ฐฐ๊ณ ํ”ˆ์ง€ ํ™•์ธ
Cat eats Fish.  // Fish ๋จน์ด๊ธฐ
Cat is not hungry. // Fish ๋จน์ด๊ธฐ
Cat is not hungry. // ๋ฐฐ๊ณ ํ”ˆ์ง€ ํ™•์ธ

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ โœ๏ธ

๋”๋ณด๊ธฐ

Animal ์ธํ„ฐํŽ˜์ด์Šค

package Di_IoC;

public interface Animal {

    /**
     * ๋™๋ฌผ์ด ๋‚ด๋Š” ์†Œ๋ฆฌ ์ถœ๋ ฅ
     */
    void makeSound();

    /**
     * ํŠน์ • ์Œ์‹์„ ๋จน๋Š” ๋™์ž‘์„ ์ถœ๋ ฅ
     * @param food - ๋™๋ฌผ์ด ๋จน๋Š” ํŠน์ • ์Œ์‹
     */
    void eat(String food);

    /**
     * ๋™๋ฌผ์ด ๋ฐฐ๊ณ ํ”ˆ ์ƒํƒœ์ธ์ง€ ๋ฐ˜ํ™˜
     * @return true/false
     */
    void isHungry();
}

Dog ํด๋ž˜์Šค

package Di_IoC;

public class Dog implements Animal{

    private boolean dogIsHungry = true;

    @Override
    public void makeSound() {
        System.out.println("Dog says : Bark!");
    }

    @Override
    public void eat(String food) {
        if (dogIsHungry) {
            System.out.println("Dog eats " + food + ".");
            this.dogIsHungry = false;
        } else {
            System.out.println("Dog is not hungry.");
        }
    }

    @Override
    public void isHungry() {
        if (dogIsHungry) {
            System.out.println("Dog is Hungry.");
        } else {
            System.out.println("Dog is not hungry.");
        }
    }
}

Cat ํด๋ž˜์Šค

package Di_IoC;

public class Cat implements Animal{

    private boolean catIsHungry = true;

    @Override
    public void makeSound() {
        System.out.println("Cat says : Meow!");
    }

    @Override
    public void eat(String food) {
        if (catIsHungry) {
            System.out.println("Cat eats " + food + ".");
            this.catIsHungry = false;
        } else {
            System.out.println("Cat is not hungry.");
        }
    }

    @Override
    public void isHungry() {
        if (catIsHungry) {
            System.out.println("Dog is Hungry.");
        } else {
            System.out.println("Dog is not hungry.");
        }
    }
}

MyHouse ํด๋ž˜์Šค

package Di_IoC;

public class MyHouse {

    private Animal animal;

    public MyHouse(Animal animal) {
        this.animal = animal;
    }

    public void makeAnimalSound() {
        this.animal.makeSound();
    }

    public void feedAnimal(String food) {
        this.animal.eat(food);
    }

    public void isHungry() {
        this.animal.isHungry();
    }
}

Main ํด๋ž˜์Šค

package Di_IoC;

public class Main {

    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();

        MyHouse houseDog = new MyHouse(dog);
        MyHouse houseCat = new MyHouse(cat);

        System.out.println("Dog house:");
        houseDog.makeAnimalSound();
        houseDog.isHungry();
        houseDog.feedAnimal("Bone");
        houseDog.feedAnimal("Bone");
        houseDog.isHungry();

        System.out.println();

        System.out.println("Cat house:");
        houseCat.makeAnimalSound();
        houseCat.isHungry();
        houseCat.feedAnimal("Fish");
        houseCat.feedAnimal("Fish");
        houseCat.isHungry();
    }
}

 

๊ณ ๋ฏผํ–ˆ๋˜ ๋ถ€๋ถ„ ๐Ÿค”

isHungry( ) ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Myhouse ํด๋ž˜์Šค์—์„œ this.animal.isHungry( )๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ ์š”๊ตฌ์‚ฌํ•ญ์—์„œ๋Š” ๋ฐฐ๊ณ ํ”ˆ ์ƒํƒœ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ๋‚˜์™€์žˆ์—ˆ๋‹ค.
isHungry( ) ๋ฉ”์„œ๋“œ๋ฅผ boolean ํƒ€์ž…์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  ๊ณ„์† ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ํŒ€์›๋ถ„๊ป˜ ๋„์›€์„ ์š”์ฒญํ–ˆ๊ณ , ์š”๊ตฌ์‚ฌํ•ญ์„ ๋˜ ์ œ๋Œ€๋กœ ์•ˆ ์ฝ๊ณ  ์ค‘๊ฐ„์— ์ฝ”๋“œ๋ฅผ ๋นผ๋จน์€ ๋‚ด ์ž˜๋ชป์ด๋ผ๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.. ์—ญ์‹œ ๋ง‰ํž ๋•Œ๋Š” ์ฃผ๋ณ€์— ๋„์›€์„ ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค.....

void ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐฐ๊ณ ํ”ˆ ์ƒํƒœ ์—ฌ๋ถ€๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ์ž‘์„ฑํ•œ ๋’ค ์ž˜๋ชป ์ž‘์„ฑํ–ˆ๋˜ eat( ) ๋ฉ”์„œ๋“œ๋„ ์ˆ˜์ •ํ•˜๊ณ  ๋‚˜๋‹ˆ๊นŒ ๊ตฌํ˜„ ๊ฒฐ๊ณผ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถœ๋ ฅ์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค!

์—ญ์‹œ.... ์š”๊ตฌ์‚ฌํ•ญ์„ ์ง„์งœ ์ œ๋Œ€๋กœ ์ฝ์–ด์•ผ๊ฒ ๋‹ค..