Il y a longtemps, dans une galaxie lointaine, lorsque les sĆurs Wachowski Ă©taient encore frĂšres, l'intelligence artificielle en la personne de l'Architecte a asservi l'humanitĂ© et créé la Matrice ... Bonjour Ă tous, voici Ă nouveau Maxim Kravets de Holyweb, et aujourd'hui je veux parler d'injection de dĂ©pendance, c'est-Ă -dire d'injection de dĂ©pendance, ou simplement de DI. Pourquoi? Peut-ĂȘtre que vous voulez juste vous sentir comme MorphĂ©e, disant le sacramentel : "Je ne peux pas vous expliquer ce qu'est DI, je ne peux que vous montrer la vĂ©ritĂ©."
Formulation du problĂšme
Ici. Jetez un Ćil Ă ces oiseaux. Il existe un programme pour les gĂ©rer. D'autres programmes contrĂŽlent les arbres et le vent, le lever et le coucher du soleil. Les programmes sont amĂ©liorĂ©s. Ils font tous leur part du travail.
Pythie
, , â , . , . â ? , . , ?
? , , , .
, , .
? , (, ), (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), .
, :
. .
. .
? , , , .
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 vous avez des questions ou des ajouts sur le sujet, je serai heureux de continuer la communication dans les commentaires. Ecrire, de quoi parler dans le prochain article ? Et si vous voulez mieux nous connaĂźtre , je suis toujours en contact avec Telegram @maximkravec .