SOLID PRINCIPLE #4

Interface Segregation

تقسيم الـ Interfaces

يعني إيه؟ زي الريموت كده! مينفعش ريموت واحد ضخم لكل الأجهزة.
كل جهاز ليه ريموت صغير خاص بيه ✂️

THE CONCEPT

مثال الريموت 📱

❌ ريموت واحد ضخم vs ✅ ريموتات صغيرة IWorker work() eat() sleep() code() design() 🤖 Robot: eat()? sleep()? 😵 IWorkable work() IEatable eat() ISleepable sleep() 🤖 Robot: IWorkable ✓ ❌ Fat Interface: الـ Robot مجبر يعمل implement لـ eat() و sleep() ✅ Segregated: كل class ياخد اللي محتاجه بس الـ Robot بياخد IWorkable - مش محتاج الباقي!

❌ Fat Interface

Interface ضخم فيه كل حاجة - الـ classes مجبرة تعمل implement لكل الـ methods حتى لو مش محتاجاها!

✅ Segregated Interfaces

Interfaces صغيرة متخصصة - كل class ياخد اللي محتاجه بس!

القاعدة: متجبرش Class يعمل implement لحاجات مش محتاجها! ✂️

الكود 💻

❌ Fat Interface
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!"); }
}
✅ Segregated
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!
}

جرب! 🎮

// اختار جهاز وشوف الـ interfaces بتاعته!
LSP DIP