Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Dependency Injection (DI)

Dependency Injection (DI) is a drug that can be injected into our bodies by means of a Dependency Injection. To understand dependency injection, let’s take an example. The CEO of an Internet startup has successfully used powerpoint to drum up investment, but he still needs a talented programmer to help him develop the product. He needs to find the programmer himself, such as a talented programmer at the byte gate.We express it in code:

class CEO {
  late Coder nbCoder;
  CEO() {
    seekCoder();
  }
  
  seekCoder() {
    // The CEO went to the big bull programmer
    nbCoder = Coder('Daniel Programmer');
  }
  
  void developProduct() {
    // Code farmers are trying to knock code developmentnbCoder.coding(); }}Copy the code

This is an ideal situation, where the CEO is on a stakeout and finds a great programmer. But more often than not, the CEO has too many things going on that he doesn’t have time to stay on the job, and he may not even know who a great programmer is (he can’t see through tartan trees, but he can carry a backpack).This situation is very tired of the CEO, he needs to find programmers, but also need to find products, but also need to find operations…… Waiting for all the people, the project is yellow! We can learn from the entertainment industry, engage in the development department CEO, in the beginning to equip him with a good programmer, the project will have a success!

class CEO {
  late Coder nbCoder;
  // Assign programmers directly
  CEO(this.nbCoder);
  
  void developProduct() {
    // Code farmers are trying to knock code developmentnbCoder.coding(); }}Copy the code

This is dependency injection! That is, when a class needs other objects, the old way is that the class creates the objects it needs, but this is inefficient and increases coupling — you need to know how to build the corresponding objects. Dependency injection, on the other hand, directly “inject” the dependent object into the class when it is created externally, so that it can be used directly in the business, regardless of how the object is built. For example, if the CEO wants a great programmer, it doesn’t matter if he’s a famous programmer — he can’t just stick to one tree.

Of course, this approach also has its drawbacks. For example, the CEO has limited that he must be a Coder (Coder class), but now we all know that even “Pan” is learning Python, so people who can write code are not necessarily programmers. For the CEO, the exact requirement is someone who can write code (knock on the blackboard: it’s important to understand real requirements).

Someone who can write code

A person who can write code literally means two things:

  • Is a person;
  • Can write code.

As for this person is male is female, age size, before do not matter (temporarily do not allow sex, age discrimination! Programmers can code until they retire. Being able to write code is a behavior, so it’s an interface to a program, and a person is naturally a class, so the CEO needs the person shown below.Convert to code:

abstact class Coding {
  void coding();
}

class Coder implements Coding {
  final String name;
  final int age;
  final Gender gender;
  
  Coder({required this.name, required this.age, required this.gender});
}

class Someone implements Coding {
  final String name;
  
  Someone({required this.name});
}

class CEO {
  late Coding personCanCoding;
  // Get someone who can write code
  CEO(this.personCanCoding);
  
  void developProduct() {
    // People who can write code work hard at codingpersonCanCoding.coding(); }}Copy the code

Here will write code there are two types, one is a serious Coder Coder, one is a human, as for age, gender do not care, will write code on the line (the initial stage will not consider so much, first coil again). Therefore, it is easy for us to develop a CEO. We can equip him with a serious coder or Someone.

void main() {
  // I am a professional programmer
  var ceo1 = CEO(Coder(name: 'Daniel Programmer', age: 28, gender: Gender.male));
  ceo1.developProduct();
  
  // I am a CEO2 student who can write code
  var ceo2 = CEO(Someone(name: '小潘'));
  ceo2.developProduct();
}
Copy the code

This shows that the CEO doesn’t care who writes the code for him (after all, few bosses care about programmers), as long as he knows that the person can write the code and get his product out there. This greatly reduces the coupling between the CEO and the person who writes the code.

conclusion

This article introduces the concept of dependency injection (DI), which is used in many situations, especially when building software frameworks, such as the well-known Spring in Java. To improve object reuse, objects are often injected into classes that need them through IoC (Inversion of control) containers. In the next article, we’ll take a real life example of dependency injection and the use of containers in GetX.

I am dao Code Farmer with the same name as my wechat official account. This is a column about the introduction and practice of Flutter, providing systematic learning articles about Flutter. See the corresponding source code here: The source code of Flutter Introduction and Practical column. If you have any questions, please add me to the wechat account: island-coder.

👍🏻 : feel the harvest please point a praise to encourage!

🌟 : Collect articles, easy to look back!

💬 : Comment exchange, mutual progress!