THE_DEVELOPER_GUIDE Telegram 5492
ู…ูู‡ูˆู… ุงู„ู€ 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.

โ€”โ€”โ€”

ูˆูู‚ูƒู… ุงู„ู„ู‡ ู„ูƒู„ ุฎูŠุฑ ๐ŸŒฟ
โค10



tgoop.com/the_developer_guide/5492
Create:
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

View MORE
Open in Telegram


Telegram News

Date: |

How to create a business channel on Telegram? (Tutorial) Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. Just at this time, Bitcoin and the broader crypto market have dropped to new 2022 lows. The Bitcoin price has tanked 10 percent dropping to $20,000. On the other hand, the altcoin space is witnessing even more brutal correction. Bitcoin has dropped nearly 60 percent year-to-date and more than 70 percent since its all-time high in November 2021. A vandalised bank during the 2019 protest. File photo: May James/HKFP. Unlimited number of subscribers per channel
from us


Telegram DevGuide ๐Ÿ‡ต๐Ÿ‡ธ
FROM American