프로그래밍 기초

의존성 주입(dependency injection) - 번역

hs-archive 2022. 10. 22. 23:28

https://www.jamesshore.com/v2/blog/2006/dependency-injection-demystified

 

James Shore: Dependency Injection Demystified

When I first heard about dependency injection, I thought, “Dependendiwhatsit?” and promptly forgot about it. When I finally took the time to figure out what people were talking about, I laughed. “That’s all it is?” “Dependency Injection” is a

www.jamesshore.com

 

 

짧은 버전

Dependency injection은 객체에 인스턴스 변수를 제공하는 것을 의미합니다. 그게 전부입니다.

 

 

약간 긴 버전 1: Dependency Non-Injection

클래스에는 메서드를 호출하는 이러한 항목이 있습니다. 이를 "종속성"이라고 부르겠습니다. 대부분의 사람들은 이것을 "변수"라고 부릅니다. 때때로 그들은 기분이 좋을 때 그것을 "인스턴스 변수"라고 부릅니다.

 

public class Example {
  // 1.
  private DatabaseThingie myDatabase;

  public Example() {
    // 2
    myDatabase = new DatabaseThingie();
  }

  public void DoStuff() {
    ...
    // 3
    myDatabase.GetData();
    ...
  }
}

 

여기, 변수가... 어, 종속성... "myDatabase"라는 이름이 있습니다. 생성자에서 초기화합니다.


약간 긴 버전 2 : Dependency Injection

원한다면 변수를 생성자에 전달할 수 있습니다. 그러면 클래스에 "종속성"이 "주입"됩니다. 이제 변수(종속성)를 사용할 때 생성한 객체가 아닌 주어진 객체를 사용합니다.

 

public class Example {
  private DatabaseThingie myDatabase;

  public Example() {
    myDatabase = new DatabaseThingie();
  }
  // 1.
  public Example(DatabaseThingie useThisDatabaseInstead) {
    myDatabase = useThisDatabaseInstead;
  }

  public void DoStuff() {
    ...
    myDatabase.GetData();
    ...
  }
}


이게 dependency injection의 전부입니다. 종속성을 인터페이스로 만든 다음 일부를 다형성으로 전달할 수 있습니다.

 

약간 긴 버전 3 : 이 작업을 수행하는 이유

무엇보다도 테스트 중에 클래스를 분리하는 데 유용합니다.

 

public class ExampleTest {
  TestDoStuff() {
    MockDatabase mockDatabase = new MockDatabase();

    // MockDatabase는 DatabaseThingie의 하위 클래스이므로 다음을 수행할 수 있습니다.
    // 여기에 "inject"합니다.
    Example example = new Example(mockDatabase);

    example.DoStuff();
    mockDatabase.AssertGetDataWasCalled();
  }
}

public class Example {
  private DatabaseThingie myDatabase;

  public Example() {
    myDatabase = new DatabaseThingie();
  }

  public Example(DatabaseThingie useThisDatabaseInstead) {
    myDatabase = useThisDatabaseInstead;
  }

  public void DoStuff() {
    ...
    myDatabase.GetData();
    ...
  }
}

 

이게 전부입니다. 종속성 주입은 실제로 인스턴스 변수를 전달하는 것일 뿐입니다.
 
 
 
 

 

정리 1 : Dependency Injection이란?

  • 객체에 인스턴스 변수를 제공하는 것

 

 

정리 2 : Dependency Injection의 장점

  • 객체 생성과 사용이 분리(사용하는 데에서 생성하지 않고 외부로부터 주입받음)되므로 추상화를 통한 코드를 만들 수 있다. 따라서 디커플링이 증가함 (SOLID 원칙 중 DIP 원칙 준수)

  • 단위 테스트를 더 쉽게 할 수 있다.

 

 

 

 

 


https://stackoverflow.com/questions/130794/what-is-dependency-injection

 

What is dependency injection?

There have been several questions already posted with specific questions about dependency injection, such as when to use it and what frameworks are there for it. However, What is dependency inject...

stackoverflow.com

https://stackify.com/dependency-injection/

 

Design Patterns Explained – Dependency Injection with Code Examples

Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID’s dependency inversion and single responsibility p

stackify.com

https://www.jamesshore.com/v2/blog/2006/dependency-injection-demystified

 

James Shore: Dependency Injection Demystified

When I first heard about dependency injection, I thought, “Dependendiwhatsit?” and promptly forgot about it. When I finally took the time to figure out what people were talking about, I laughed. “That’s all it is?” “Dependency Injection” is a

www.jamesshore.com

 

'프로그래밍 기초' 카테고리의 다른 글

int와 Integer의 차이  (0) 2022.11.05
커플링(coupling), 디커플링(decoupling) - 번역  (0) 2022.10.22
dist 폴더  (0) 2022.10.13
CLI란?  (0) 2022.10.13
수열  (0) 2022.07.04