tgoop.com/the_developer_guide/5492
Last Update:
ู
ูููู
ุงูู Dependency Inversion Principle ๐ก
.
.
ููู ู
ุจุฏุฃ ู
ู ู
ุจุงุฏุฆ SOLID ุจูุบููุฑ ุทุฑููุฉ ุชูููุฑู ูู ุชุตู
ูู
ุงูููุฏ ุจุดูู ูุจูุฑ ุฌุฏูุง...
ู
ุจุฏุฃ ุฃูู ู
ุง ุชููู
ู ูููุณ ูุชุทุจููู ุตุญุ ูุชุญุณ ุฅู ุงูู
ุดุฑูุน ุจูู modular ุฃูุชุฑุ ูุงูู testing ุจูู ุฃุณููุ ูุงูู bugs ุจูุช ููููุฉ ุฅูู ุญุฏ ู
ุง...
ุชุนุงู ูุฏุฑุฏุด ุดููุฉ ุนู ู
ุจุฏุฃ ุงูู Dependency Inversion...
โโโ
๐ ูุนูู ุฅูู Dependency Inversion Principleุ
ุงูู
ุจุฏุฃ ุฏู ุจูููู:
"High-level modules should not depend on low-level modules. Both should depend on abstractions."
ู
"Abstractions should not depend on details. Details should depend on abstractions."
ูุนูู ูู
ุง ุชูุฌู ุชุจูู ุฌุฒุก ูุจูุฑ ู
ู ุงูุณูุณุชู
(ุฒู ู
ุซูุงู order service ูู ุชุทุจูู ุชุฌุงุฑุฉ ุฅููุชุฑูููุฉ)ุ ุงูู
ูุฑูุถ ู
ูููุด ุงูู high-level logic (ุฒู ุฅุฒุงู ุงูู order ุจูุชู
) ุจูุนุชู
ุฏ ู
ุจุงุดุฑุฉ ุนูู ุงูู details ุฒู ู
ุซูุง API ู
ุนูู ุฃู database ู
ุนููุฉ ุฃู class ุจุชุจุนุช ุฅูู
ููุงุช.
ุจุฏู ูุฏูุ ุงูู
ูุฑูุถ ูููู ุจูุนุชู
ุฏ ุนูู abstraction (interface ุฃู contract)ุ ุจุญูุซ ุงูุชูุงุตูู ุฏู ุชูุฏุฑ ุชุชุบูุฑ ุจุณูููุฉ ุจุนุฏ ูุฏู ู
ู ุบูุฑ ู
ุง ุชุบููุฑ ูู ุงูู business logic ููุณู.
โโโ
๐ฆ ู ุซุงู ุจุณูุท:
[ ูู ุงูุฃููุงุฏ ูู ุงูุชุนูููุงุช ๐ ]
class EmailService {
sendEmail(to: string, body: string) {
// logic to send email
}
}
class OrderService {
private emailService = new EmailService();
placeOrder(orderData: any) {
// logic to place order
this.emailService.sendEmail(orderData.customerEmail, "Order placed!");
}
}ูุฏู ุงูู OrderService ู ุนุชู ุฏ ุจุดูู ู ุจุงุดุฑ ุนูู ุงูู EmailService.
ูู ุญุจูุช ุชุบูุฑ ูุณููุฉ ุฅุฑุณุงู ุงูุฅูู ูู ุฃู ุชุจุนุชูุง ุนุจุฑ SMS ุฃู push notificationุ ูุชุถุทุฑ ุชุบููุฑ ูู ุงูููุฏ ุจุชุงุน OrderService ููุณูโฆ ูุฏู ุถุฏ ู ุจุฏุฃ open/closed principle ูู ุงู.
โโโ
โ ุงูุญูุ
interface Notifier {
notify(to: string, message: string): void;
}
class EmailService implements Notifier {
notify(to: string, message: string) {
// send email
}
}
class SMSService implements Notifier {
notify(to: string, message: string) {
// send sms
}
}
class OrderService {
constructor(private notifier: Notifier) {}
placeOrder(orderData: any) {
// logic to place order
this.notifier.notify(orderData.customerContact, "Order placed!");
}
}ูุฏู ุงูู OrderService ู ูุนุฑูุด ุฃู ุญุงุฌุฉ ุนู ุงูู implementation ุจุชุงุน ุงูู notifierุ ุณูุงุก ูุงู email ุฃู sms.
ูู ุจุณ ุจูุชุนุงู ู ู ุน abstraction (interface ุงุณู ูุง Notifier).
ูุจุงูุชุงูู ุชูุฏุฑ ุชุบูุฑ ุงูู implementation ูู ุฃู ููุช ู ู ุบูุฑ ู ุง ุชูู ุณ ุงูู OrderService.
โโโ
๐ก ุฅุฒุงู ุฏู ูููุฑู ู ุนุงูุ
- ุงูููุฏ ุจุชุงุนู ุจูู loosely coupled.
- ุจูู modular ูุฃุณูู ูู ุงูุชุนุฏูู ูุงูุตูุงูุฉ.
- ุงูู testing ุจูู ุฃุจุณุท ูุฃูู ุชูุฏุฑ ุชุนู ู mock ูู Notifier ุจุณูููุฉ.
- ุจููุช ุชูุฏุฑ ุชุจุฏูู ุงูู implementation ู ู ุบูุฑ ู ุง ุชุนู ู refactor ุชููู.
โโโ
ุงูู Dependency Inversion ุจูุฎููู ุฏุงูู ูุง ุชููุฑ ูู dependencies ุนูู ุฅููุง ุดูุก ู ู ูู ูุชุบูุฑโฆ ูุจุฏู ู ุง ุชุจูู ุนูููุง ุจุดูู ู ุจุงุดุฑุ ุงุณุชุฎุฏู abstraction ุชูุตู ุจู ุจูู high-level logic ู low-level details.
โโโ
ููููู ุงููู ููู ุฎูุฑ ๐ฟ
BY DevGuide ๐ต๐ธ
Share with your friend now:
tgoop.com/the_developer_guide/5492
