Sumergirse en la inyección de dependencia (DI) o cómo romper la matriz

Hace mucho tiempo en una galaxia lejana, cuando las hermanas Wachowski aún eran hermanas, la inteligencia artificial en la persona del Arquitecto esclavizó a la humanidad y creó la Matriz ... Hola a todos, soy Maxim Kravets de Holyweb nuevamente, y hoy quiero hablar de inyección de dependencia, es decir, de inyección de dependencia, o simplemente DI. ¿Para qué? Quizás solo quieras sentirte como Morfeo, diciendo el sacramental: "No puedo explicarte qué es DI, solo puedo mostrarte la verdad".  





Formulación del problema

Aquí. Eche un vistazo a estas aves. Hay un programa para gestionarlos. Otros programas controlan los árboles y el viento, el amanecer y el atardecer. Se están mejorando los programas. Todos hacen su parte del trabajo.





pitia





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





? , , , .





, , .





? , (, ), (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. 





Si tiene alguna pregunta o adiciones sobre el tema, estaré encantado de seguir comunicándome en los comentarios. Escriba, ¿qué contar en el próximo artículo? Y si quieres conocernos mejor , siempre estoy en contacto en Telegram @maximkravec








All Articles