Mergulhando na injeção de dependência (DI), ou como quebrar a matriz

Há muito tempo atrás em uma distante Galáxia, quando as irmãs Wachowski ainda eram irmãs, a inteligência artificial na pessoa do Arquiteto escravizou a humanidade e criou a Matriz ... Olá a todos, aqui é Maxim Kravets de Holyweb novamente, e hoje eu quero falar sobre injeção de dependência, ou seja, sobre injeção de dependência, ou apenas DI. Pelo que? Talvez você queira apenas se sentir como Morfeu, dizendo o sacramental: "Não posso lhe explicar o que é DI, só posso lhe mostrar a verdade."  





Formulação do problema

Aqui. Dê uma olhada nesses pássaros. Existe um programa para gerenciá-los. Outros programas controlam árvores e vento, nascer e pôr do sol. Os programas estão sendo aprimorados. Todos eles fazem sua própria parte do trabalho.





Pítia





, , —  , . , . —  ? , . , ?





? , , , .





, , .





? , (, ), (Injection) , , , . 





, , : , ? — (Dependency) .





? — . Dependency Injection — . — , ? 





, : « ». — . . ? . 





, - — , ! 





. , , , ! — , .





, . , , . DI — . . matrix, , . , , , , , , whoWin():





class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };

  human = {
    name: 'Cypher',
    damage: 100,
  };

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
}

const matrixV1 = new Matrix();
console.log(‘ ’, matrixV1.whoWin());
      
      



, , , .





  Smith
      
      



, , , , - — . , , . — . , . .





class Human {
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage;
  }
}

class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };
 human;

  constructor(challenger) {
    this.human = challenger;
  }

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
      
      



Human, , . — . , ?





const Trinity = new Human('Trinity', 500);
const matrixV1 = new Matrix(Trinity);
console.log(' ', matrixV1.whoWin());
      
      



, « » (), , , .





  Smith
      
      



! , ? , Matrix Human! , , , !





class Human {
  get damage(): number {
    return this.damage * 1000;
  }
}
      
      



...





?

? , Matrix challenger, , damage, . , , ! — . ? , damage, power? strength?





! Dependency inversion principle, (DIP). , , , «» , Dependency inversion (DI), .





, :





  1. . .





  2. . .





? , , , .





Matrix AbstractHuman, Human — :





abstract class AbstractHuman {
  abstract get name(): string;
  abstract get damage(): number;
}

class Human implements AbstractHuman{
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage;
  }
}


class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };
 human;

  constructor(challenger: AbstractHuman) {
    this.human = challenger;
  }

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
}

const Morpheus = new Human('Morpheus', 900);
const matrixV2 = new Matrix(Morpheus);
console.log(' ', matrixV2.whoWin());
      
      



, — .





  Smith
      
      



, ? Matrix Human — . Human , — «» AbstractHuman () , . .





, ! , , — … , .





      Smith
      Smith
      
      



, , , . , :





...
class TheOne implements AbstractHuman{
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage * 1000;
  }
}
const Neo = new TheOne('Neo, 500);
const matrixV5 = new Matrix(Neo);
      
      



!





      
      
      



, ? Matrix, Human, . . — . 





, , , , . , , !





, , — Inversion of Control (IoC). 





, , (, ) (, ). ( ) — . 





, DIP ( ) — IoC.





- -?

—  . , singleton multiton — (), (). 





, .





  • ,





  • , ,





  • ( ),





  • , , .





: - / (Service Locator), - DI, IoC Container. .





, (). , . Angular —  Injectable.





@Injectable()
export class SomeService {}
      
      



IoC . 





SomeService, , . 





-, —

—  , . , , . , «» , , . , , .





—  , « » « - ». , . new, «», , .





, ?

, «» - , IoC?  





1 — .





  • , -. 





  • DI. .





  • , .





  • — , , «», — IoC . 





2 — .





  • — , -.





  • — , Y. 





  • , Y , X.





  • — , , . .





3 — .





  • — .





  • production — «» .





  • — , .





  • — , , .





, , , DI, . . —  , . , , , DI. 





Se você tiver alguma dúvida ou acréscimo sobre o tema, terei o maior prazer em continuar a comunicação nos comentários. Escreva, o que contar no próximo artigo? E se você quiser nos conhecer melhor , estou sempre em contato pelo Telegram @maximkravec








All Articles