<aside>
๐ก Dependency Injection(์์กด์ฑ ์ฃผ์
, ์ดํ DI)์ ๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๋ฅผ ๋น ์ค์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์ปจํ
์ด๋๊ฐ ์๋์ ์ผ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. ์ด๋ฅผ ํตํด ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์์ผ๋ฉฐ ์ด๋ก ์ธํด ์ ์ง๋ณด์์ฑ๊ณผ ์ ์ฐ์ฑ์ด ์ฆ๊ฐํ๋ค.
</aside>
public class A {
private B b = new B();
}
public class B {
}
class A ๊ฐ class B ๋ฅผ ํ๋๋ก ๊ฐ์ง ๋ A๋ B์ ์์กดํ๊ฒ ๋๋ค.
public class A {
/* ์ปดํ์ผ ์๋ฌ ๋ฐ์ */
private B b = new B();
}
/* ํด๋์ค๋ช
์ด B์์ NewB๋ก ๋ณ๊ฒฝ */
public class NewB {
}
์์กด์ฑ์ด ๊ฐํ๋ค๋ ๊ฒ์ ํ ๊ฐ์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ฉด ์ด์ ์์กดํ๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ค๋ ํจ๊ป ๋ณ๊ฒฝ๋์ด์ผ ํ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค. B๊ฐ NewB ๋ก ๋ณ๊ฒฝ๋๋ฉด ํด๋น ํด๋์ค๋ฅผ ํ๋๋ก ๊ฐ์ง๊ณ ์๋ A ๋ ๋ณ๊ฒฝ๋์ด์ผ ํ ๊ฒ์ด๋ค. ์ด์ฒ๋ผ ๊ฐ์ฒด ๊ฐ์ ์์กด ๊ด๊ณ๊ฐ ๊ฐํ๊ฒ ๋ฌถ์ฌ์์ ๋ ๊ฒฐํฉ๋ ๊ฐ ๋๋ค๊ณ ํํํ๋ค. ์ด๋ก ์ธํด ์ ์ง๋ณด์์ฑ๊ณผ ์ ์ฐ์ฑ์ด ์ ํ๋ ์ ์๋ค.
public class A {
/* ์์ ํ์
์ ํ๋๋ก ์ค์ */
private B b;
/* ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์์ฑ์๋ฅผ ํตํด ์ ๋ฌ ๋ฐ์ */
public A(B b) {
this.b = b;
}
}
/* ์์ ํ์
์ผ๋ก ์ฌ์ฉํ ์ธํฐํ์ด์ค */
public interface B {
}
/* ์ธํฐํ์ด์ค์ ๊ตฌํ ํด๋์ค */
public class NewB implements B {
}
public class A {
/* ์์ ํ์
์ ํ๋๋ก ์ค์ */
private B b;
/* ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์์ฑ์๋ฅผ ํตํด ์ ๋ฌ ๋ฐ์ */
public A(B b) {
this.b = b;
}
}
/* ์์ ํ์
์ผ๋ก ์ฌ์ฉํ ์ธํฐํ์ด์ค */
public interface B {
}
/* ์ธํฐํ์ด์ค์ ๊ตฌํ ํด๋์ค */
public class NewB implements B {
}
์ด์ ์ ์ฝ๋์ ๋น๊ตํ๋ฉด NewB ๋ผ๋ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ์ฒด์ ํ์
์ ์ฌ์ฉํ๋ ๋์ B ๋ผ๋ ์์ ์ธํฐํ์ด์ค ํ์
์ผ๋ก ํ๋๋ฅผ ์ ์ธํ๋ค. ๋ํ ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ตฌ๋ฌธ๋ ์์ด์ก๊ณ ์์ฑ์๋ฅผ ํตํด ์ ๋ฌ ๋ฐ๋๋ก ํ๋ค. ์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋ฉด ์ค์ ๋ก ์ฌ์ฉํ๋ ๊ตฌํ์ฒด๊ฐ NewB ์์ ๋ ๋ค๋ฅธ ํ์
์ผ๋ก ๋ณ๊ฒฝ ๋๋๋ผ๋ A ์ ์ฝ๋๋ ๋ณ๊ฒฝ ๋ ํ์๊ฐ ์๋ค. ์์กด ๊ด๊ณ๊ฐ ๋์จํด์ง๊ณ ๊ฒฐํฉ๋๊ฐ ๋ฎ์์ก๋ค๊ณ ํ ์ ์๋ค.
์๋ ์ฝ๋๋ ํ
์คํธ์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ ํ Account, PersonalAccount, MemberDTO ํด๋์ค์ด๋ค.
Account
public interface Account {
/* ์์ก ์กฐํ */
String getBalance();
/* ์
๊ธ */
String deposit(int money);
/* ์ถ๊ธ */
String withDraw(int money);
}
PersonalAccount
@Data
public class PersonalAccount implements Account {
private final int bankCode; //์ํ์ฝ๋
private final String accNo; //๊ณ์ข๋ฒํธ
private int balance; //์์ก
@Override
public String getBalance() {
return this.accNo + " ๊ณ์ข์ ํ์ฌ ์์ก์ " + this.balance + "์ ์
๋๋ค.";
}
@Override
public String deposit(int money) {
String str = "";
if(money >= 0) {
this.balance += money;
str = money + "์์ด ์
๊ธ๋์์ต๋๋ค.";
} else {
str = "๊ธ์ก์ ์๋ชป ์
๋ ฅํ์
จ์ต๋๋ค.";
}
return str;
}
@Override
public String withDraw(int money) {
String str = "";
if(this.balance >= money) {
this.balance -= money;
str = money + "์์ด ์ถ๊ธ๋์์ต๋๋ค.";
} else {
str = "์์ก์ด ๋ถ์กฑํฉ๋๋ค. ์์ก์ ํ์ธํด์ฃผ์ธ์.";
}
return str;
}
}
MemberDTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MemberDTO {
private int sequence; //ํ์๋ฒํธ
private String name; //์ด๋ฆ
private String phone; //ํด๋ํฐ๋ฒํธ
private String email; //์ด๋ฉ์ผ
private Account personalAccount; //๊ฐ์ธ๊ณ์ข
}
@Bean
public Account accountGenerator() {
return new PersonalAccount(20, "110-234-567890");
}
@Bean
public MemberDTO memberGenerator() {
/* MemberDTO ์์ฑ์๋ฅผ ํตํด Account๋ฅผ ์์ฑํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ฆฌํด ๊ฐ์ ์ ๋ฌํ์ฌ bean์ ์กฐ๋ฆฝํ ์ ์๋ค. */
return new MemberDTO(1, "ํ๊ธธ๋", "010-1234-5678", "[email protected]", accountGenerator());
}
โ Account(๊ณ์ข) ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ PersonalAccount(๊ฐ์ธ๊ณ์ข) ํด๋์ค๊ฐ ์๊ณ MemberDTO ๋ Account ํ์
์ ํ๋๋ก ๊ฐ์ง๊ณ ์๋ค. (MemberDTO ๋ Account ํ์
์ ์์กดํ๋ค.)