الانتقال إلى المحتوى
Software ArchitectureSOLID· 8 دقائق للقراءة

الـ Liskov Substitution بيتكلم عن السلوك مش عن inheritance بيعمل compile

الـ compiler بيشيّك على الـ signatures. أما إن الـ type يتبدّل بأمان فده معتمد على اللي الـ callers بيعتمدوا عليه: الـ inputs المقبولة، والنتايج الموعودة، والـ exceptions اللي لازم يتعاملوا معاها.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

الـ support بلّغ إن refund لقطعة واحدة من order فيه تلات قطع بيفشل، بس لما الـ order يكون مدفوع بـ voucher. الـ VoucherGateway بتعمل inherit من PaymentGateway، وبتعمل override لـ RefundAsync، وبتعمل compile، والـ tests بتاعتها بتعدّي. وكمان بترمي exception لأي مبلغ غير المبلغ اللي اتخصم كله. والـ RefundService اتكتب على PaymentGateway، والـ contract بتاعها بيسمح بـ partial refunds، فعمره ما توقع ده. وأول حل اتقترح في الـ review كان if (gateway is VoucherGateway).

الفكرة الغلط الشائعة

الفكرة بتقول إن لو class بتعمل inherit من base class، أو بتعمل implement لـ interface، والكود بيعمل compile، يبقى ينفع تستخدمها في أي مكان مستني الـ base type. والـ Liskov Substitution Principle متحقق تلقائي.

والفكرة منطقية لأن الـ compiler بيشيّك فعلًا على حاجات كتير: أسماء الـ methods، وأنواع الـ parameters والـ return، والـ overrides، والـ accessibility. و Liskov و Wing موافقين إن ده مطلوب: "Clearly subtypes must provide the expected methods with compatible signatures." وكمان tutorials كتير بتشرح الـ LSP على إنه قاعدة عن الـ class hierarchies، و Robert C. Martin نفسه بيعترف: "People (including me) have made the mistake that this is about inheritance."

النص الصح في الكلام: الـ signatures المتوافقة شرط لازم. بس زي ما الـ paper بيقول، الـ type checking "captures only a small part of what it means for a program to be correct".

اللي بيحصل فعلًا

التبديل بيتقاس باللي الـ callers يقدروا يلاحظوه

الـ paper بتاع Liskov و Wing سنة 1994 بيطرح السؤال كده: الـ objects بتاعة الـ subtype "ought to behave the same as those of the supertype as far as anyone or any program using supertype objects can tell." والـ Subtype Requirement بتاعهم بيقول إن أي property تقدر تثبتها عن objects الـ supertype لازم تفضل صح عن objects الـ subtype. وصياغة Martin سنة 2020 للـ أقصر: "A program that uses an interface must not be confused by an implementation of that interface."

الـ signatures مش هتفرّق بين stack و queue

المثال بتاع الـ paper نفسه: الـ stack والـ queue ممكن الاتنين يبقى فيهم put بتضيف عنصر و get بتشيل عنصر. وبقواعد الـ signatures، أي واحد فيهم ممكن يبقى subtype قانوني للتاني. بس "a program written in the expectation that x is a stack is unlikely to work correctly if x actually denotes a queue, and vice versa." نفس الشكل، وسلوك مختلف، والـ compiler مش شايف الفرق.

قواعد السلوك

الـ paper بيحوّل "يتصرف بنفس الطريقة" لقواعد تقدر تشيّك عليها لكل method الـ subtype بيقدمها:

  • الـ : الـ method بتاعة الـ subtype لازم تشتغل في كل حالة الـ method بتاعة الـ supertype بتقبلها. ممكن تقبل أكتر، عمرها ما تقبل أقل. والـ VoucherGateway بتكسر دي لما ترفض المبالغ الجزئية.
  • الـ : الـ subtype ممكن يضمن أكتر من الـ supertype، عمره ما يضمن أقل.
  • الـ Exceptions: الـ method بتاعة الـ subtype ماينفعش ترمي exceptions الـ supertype مابيرميهاش، "since a caller of a method on a supertype object should not expect to handle an unknown exception."
  • الـ Invariants والـ history: اللي دايمًا صح عن الـ supertype يفضل صح. والـ paper بيوضّح إن set تقدر تمسح منها مش subtype من set بتكبر بس، حتى لو الـ signatures متطابقة.

الموضوع مالوش علاقة بالـ inheritance أصلًا

الـ paper بيوضّح إن الـ subtype لما يقدّم methods الـ supertype مش معناه إنه بيورث الكود بتاعها، "simply that it provides methods with the same behavior". و Martin بيقول نفس النقطة من الناحية التانية: الـ LSP عن الـ subtyping، وكل الـ implementations بتاعة الـ interfaces هي subtypes من الـ interface، والـ duck types هي subtypes من interface ضمني. وتحذيره عن التمن: لو implementation لخبطت اللي بيستخدم الـ base type، "if/switch statements will proliferate."

الحل: اوعد باللي كل implementation تقدر تحافظ عليه بس

C#
public class PaymentGateway{    // Contract: refunds any amount up to what was captured    public virtual Task RefundAsync(Payment p, decimal amount) =>        Task.CompletedTask; // stand-in for the provider's refund call}public sealed class VoucherGateway : PaymentGateway{    // Compiles, but demands more than the base type promised    public override Task RefundAsync(Payment p, decimal amount) =>        amount == p.Captured            ? base.RefundAsync(p, amount)            : throw new InvalidOperationException("Full refunds only");}// Refunding one item of three now fails only for voucher orders,// and RefundService starts growing: if (gateway is VoucherGateway)

الفرق في السلوك بقى جزء من الـ types. الـ card gateways بتعمل implement لـ IPartialRefunds، والـ voucher gateway بتعمل implement لـ IRefunds بس، والـ contract بتاعها عمره ما وعد بـ partial refunds. والـ RefundService بياخد IPartialRefunds لمرتجعات القطع ومش محتاج أي type checks. والـ bug اتنقل من الـ production للـ compiler، وده أحسن مكان تمسك فيه وعد مكسور.

الفكرة الغلطالحقيقة
لو بيعمل compile يبقى ينفع يتبدّلالـ compiler بيشيّك على الـ signatures، والـ LSP عن السلوك اللي الـ callers معتمدين عليه
الـ LSP عن الـ inheritanceMartin: عن الـ subtyping، ومنه الـ interface implementations والـ duck types
الـ subtype ممكن يبقى أشد في الـ inputsممكن يقبل أكتر، عمره ما يقبل أقل، وممكن يوعد بأكتر، عمره ما يوعد بأقل
رمي exception جديدة في override عاديالـ callers بتوع الـ base type مش متوقعينها، و Liskov و Wing بيمنعوا الـ exceptions الجديدة
مشاركة الكود سبب كويس تعمل subclassالـ subtyping محتاج نفس السلوك، وإعادة استخدام الكود مش هي المقياس

تطبقها إزاي في مشروعك

  1. اكتب الـ contractفي الـ doc comments بتاعة الـ base type، وضّح أنهي inputs صح، وكل method بتضمن إيه، وأنهي exceptions الـ callers لازم يتعاملوا معاها.
  2. دوّر على العلاماتدوّر على overrides بترمي NotSupportedException أو مابتعملش حاجة، وعلى callers بيشيّكوا على types محددة بـ is أو as أو type switches.
  3. شارك الـ contract testsشغّل نفس الـ test suite على كل implementation لـ interface أو base class، ومنهم الـ fakes اللي بتستخدمها في tests تانية.
  4. قسّم الوعود اللي مش الكل بيحافظ عليهالما implementation ماتقدرش تحترم جزء من الـ contract، ادّي الجزء ده interface لوحده، أو استخدم composition بدل الـ inheritance.

الخلاصة

  • الـ LSP عن السلوك: الكود اللي بيستخدم type ماينفعش يتفاجئ من أي subtype ليه.
  • الـ signatures المتوافقة شرط لازم بس مش كفاية، والـ stack والـ queue ممكن يبقى ليهم نفس الـ signatures.
  • الـ subtype ممكن يقبل أكتر ويوعد بأكتر، بس عمره ما يطلب أكتر أو يدّي أقل أو يرمي exceptions جديدة.
  • بينطبق على الـ interfaces والـ duck types، مش بس على الـ class inheritance.
  • الـ type checks على subtypes والـ overrides اللي بترفض تشتغل علامات إن الـ contract غلط، قسّمه.

📚 المصادر

  1. A Behavioral Notion of Subtyping — Barbara H. Liskov and Jeannette M. Wing, ACM TOPLAS (1994)
  2. Solid Relevance — Robert C. Martin, The Clean Code Blog (2020)
  3. ICollection<T>.Add(T) Method — .NET API reference, Microsoft Learn
  4. ICollection<T>.IsReadOnly Property — .NET API reference, Microsoft Learn

التعليقات (0)