
الـ Liskov Substitution بيتكلم عن السلوك مش عن inheritance بيعمل compile
- كاتب موثّق
محتويات المقال
VoucherGateway بتعمل inherit من PaymentGateway، وبتعمل override لـ RefundAsync، وبتعمل compile، والـ tests بتاعتها بتعدّي. وكمان بترمي exception لأي مبلغ غير المبلغ اللي اتخصم كله. والـ RefundService اتكتب على PaymentGateway، والـ contract بتاعها بيسمح بـ partial refunds، فعمره ما توقع ده. وأول حل اتقترح في الـ review كان if (gateway is VoucherGateway).
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
التبديل بيتقاس باللي الـ callers يقدروا يلاحظوه
الـ signatures مش هتفرّق بين 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 مش شايف الفرق.
قواعد السلوك
الـ : الـ 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 أصلًا
الحل: اوعد باللي كل implementation تقدر تحافظ عليه بس
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)// Each contract promises only what all of its implementations keeppublic interface IRefunds{ Task RefundInFullAsync(Payment p);}public interface IPartialRefunds : IRefunds{ Task RefundAsync(Payment p, decimal amount); // any amount <= Captured}public sealed class VoucherGateway : IRefunds{ public Task RefundInFullAsync(Payment p) => Task.CompletedTask;}// Code that refunds single items asks for IPartialRefunds,// so passing a VoucherGateway is now a compile errorIPartialRefunds، والـ voucher gateway بتعمل implement لـ IRefunds بس، والـ contract بتاعها عمره ما وعد بـ partial refunds. والـ RefundService بياخد IPartialRefunds لمرتجعات القطع ومش محتاج أي type checks. والـ bug اتنقل من الـ production للـ compiler، وده أحسن مكان تمسك فيه وعد مكسور.
تطبقها إزاي في مشروعك
اكتب الـ contract في الـ doc comments بتاعة الـ base type، وضّح أنهي inputs صح، وكل method بتضمن إيه، وأنهي exceptions الـ callers لازم يتعاملوا معاها. دوّر على العلامات دوّر على overrides بترمي NotSupportedExceptionأو مابتعملش حاجة، وعلى callers بيشيّكوا على types محددة بـisأوasأو type switches.شارك الـ contract tests شغّل نفس الـ test suite على كل implementation لـ interface أو base class، ومنهم الـ fakes اللي بتستخدمها في tests تانية. قسّم الوعود اللي مش الكل بيحافظ عليها لما implementation ماتقدرش تحترم جزء من الـ contract، ادّي الجزء ده interface لوحده، أو استخدم composition بدل الـ inheritance.
الخلاصة
الـ LSP عن السلوك: الكود اللي بيستخدم type ماينفعش يتفاجئ من أي subtype ليه. الـ signatures المتوافقة شرط لازم بس مش كفاية، والـ stack والـ queue ممكن يبقى ليهم نفس الـ signatures. الـ subtype ممكن يقبل أكتر ويوعد بأكتر، بس عمره ما يطلب أكتر أو يدّي أقل أو يرمي exceptions جديدة. بينطبق على الـ interfaces والـ duck types، مش بس على الـ class inheritance. الـ type checks على subtypes والـ overrides اللي بترفض تشتغل علامات إن الـ contract غلط، قسّمه.
التعليقات (0)