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) Matt Hussey, editorial director at NEAR Protocol also responded to this news with โ€œ#meIRLโ€. Just as you search โ€œBear Market Screamingโ€ in Telegram, you will see a Pepe frog yelling as the groupโ€™s featured image. Unlimited number of subscribers per channel Hashtags
from us


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