يعني إيه؟
زي الريموت كده! مينفعش ريموت واحد ضخم لكل الأجهزة.
كل جهاز ليه ريموت صغير خاص بيه ✂️
Interface ضخم فيه كل حاجة - الـ classes مجبرة تعمل implement لكل الـ methods حتى لو مش محتاجاها!
Interfaces صغيرة متخصصة - كل class ياخد اللي محتاجه بس!
القاعدة: متجبرش Class يعمل implement لحاجات مش محتاجها! ✂️
interface IWorker {
work();
eat();
sleep();
}
class Human implements IWorker {
work() { console.log("Working"); }
eat() { console.log("Eating"); }
sleep() { console.log("Sleeping"); }
}
class Robot implements IWorker {
work() { console.log("Working 24/7"); }
// ❌ Robots don't eat!
eat() { throw new Error("No!"); }
// ❌ Robots don't sleep!
sleep() { throw new Error("No!"); }
}
interface IWorkable { work(); }
interface IEatable { eat(); }
interface ISleepable { sleep(); }
// ✅ Human implements all
class Human implements
IWorkable, IEatable, ISleepable {
work() { console.log("Working"); }
eat() { console.log("Eating"); }
sleep() { console.log("Sleeping"); }
}
// ✅ Robot only implements what it can
class Robot implements IWorkable {
work() { console.log("Working 24/7"); }
// No eat(), no sleep() - perfect!
}