يعني إيه؟
زي الفيشة الكهربائية! اللابتوب مش بيعتمد على شاحن معين...
بيعتمد على الـ Interface (الفيشة) - أي شاحن يشتغل! 🔌
الـ NotificationService - مش عايز يعرف هيبعت email ولا SMS. عايز بس INotifier يـ send()!
INotifier - العقد بين الـ Service والـ Implementation. الاتنين بيعتمدوا عليه!
Email, SMS, Push - كل واحد يـ implement الـ INotifier. سهل تضيف جديد!
class EmailSender {
send(msg) {
console.log(`📧 Email: ${msg}`);
}
}
class NotificationService {
constructor() {
// ❌ مربوط بـ EmailSender!
this.sender = new EmailSender();
}
notify(msg) {
this.sender.send(msg);
}
}
// ❌ عايز SMS؟ لازم تغير الكود!
// Interface
class INotifier {
send(msg) { throw "implement!"; }
}
class Email extends INotifier {
send(msg) { return `📧 ${msg}`; }
}
class SMS extends INotifier {
send(msg) { return `📱 ${msg}`; }
}
// ✅ بياخد الـ notifier من بره!
class NotificationService {
constructor(notifier) {
this.notifier = notifier;
}
notify(msg) {
return this.notifier.send(msg);
}
}
// سهل التبديل!
new NotificationService(new Email());
new NotificationService(new SMS());