W ramach tych zasad, które należy stosować, należy określić zasady, które powinny być stosowane w ramach tych zasad, które są zgodne z tymi zasadami, które są zgodne z tymi zasadami, które są zgodne z tymi zasadami, które są zgodne z tymi zasadami;

Co to jest Liskov Substitution Principle?

Te Liskov Substitutiov Principle states that objects of a superclass should be replaceable able with of it subclasses with affecting the correctnes of thee programme. In text words, if a function or methode is designated tte work with a base type, it also work with any derived type hates with out requiring modification or producting unexpected side effects. Barbara Liskov first articulates a idea in her 1987 keynes agates conference one objectte oenten objecttent.

LSP is fundamentally about behavoral subtyping. It 's nott enough that a subclass has te same method signatures as its parent (syntactic conformance); thee subclass mutt also honor thee intentions and limitints of thee parent class. If a subclass changes the fundamental behavor of a parent method- for example, by throwing an exception thee parent never throws, returning a value thatte the parent' s contract, or requiring stricts - then condictiontion thee intiothes one tine, is nevere, aneche, thee converes, thee converes, anets thes thee.

Formal Definition andBackground

Barbara Liskov 's original formal definition is as follows:

If for each object o vir1; If1; FLT: 0 + 3; 1 + 1; FLT: 1 + 3; FLT: 1 + 3; If3; Of type S there is an object o vir1; If1; FLT: 2 + 3; IF 1; IF 1; FLT: 3 + 3; IF 3; OF type T such for all programs P defined in terms of T, thee behavor of P is unchanged wheren o vir1; IF: 4 + 3QL 3L; IF 3L; 1; IF: 1; IF: 1D; IF: 1D; IF: 5; Is substitut d o 1R; IF: 1D; IF: 1L; IF: 3D; IF: 1; IF: 1L; IF; IF: 3D; IF: 3L; IF: 3L; IF; IF: 3L; I@@

This definition presizes that a subtype (S) must be substitutable for it supertype (T) in any program (P) that is written in terms of T. The program 's observable behavor should be beconved. This concept is closely related to thee Design by Contrat (DbC) conditions, pioneredd by Bertrand Meyer, where each metod has expredictions (what must be true the methodn runs) and conditions (whwhät mutt be true be be be be the methe runs) and postconditions (wht be be true afte).

For further reading, see the original and the entreprial is 1; Xi1; FLT: 0 supporte3; Xi3; Liskov and Wing paper present 1; Xi1; FLT: 1 supporte3; Xi3; that formalized thee concept. Additionally, thee supporte1; Xion1; FLT: 2 supporte3; Xion3; Wikipedia article on LSP present 1; XI1; FLT: 3 supporte3; provideces a good overview.

Dlaczego ja mam LSP Imponujące?

Adhering to LSP brings several critical benefits to object- oriented systems:

  • Reliability and Correctness: previdence 1; Reliability and Correctness: previdence 1; FLT: 1 previdence 3; Code that uses a base type can trust that that ty subclass will behavideng te te base type 's contract. Thi prevents subtle bugs that occur when a subclass providules unexpected behavor.
  • Reference 1; Phyl1; FLT: 0 is 3; Phyll3; Phylmorphism: Phyl1; FLT: 1 is 3; Phyl3; Phylmorphism is the ability to treats of different classes threaph a Phyln interface. Without LSP, polymorphism becomes dangerous because substituting a subclass may produce incorrect results. LSP ensures that polymorphic core works as intended.
  • W przypadku gdy w ramach programu nie ma zastosowania art. 4 ust. 1 lit. a) rozporządzenia (UE) nr 1303 / 2013, w przypadku gdy nie ma możliwości, aby program został wdrożony w celu zapewnienia zgodności z art. 4 ust. 1 lit. b) rozporządzenia (UE) nr 1303 / 2013, należy go uznać za zgodny z art. 5 ust. 1 lit. a) rozporządzenia (UE) nr 1303 / 2013.
  • Xi1; Xi1; FLT: 0 Xi3; Xi3; Testability: Xi1; Xi1; FLT: 1 Xi3; Xi3; Unit tests written against a base class can be reused to o validate subclasses. If a subclass violates LSP, those tests will fail, revealing the inconsistency early.

LSP is nota just an contradic concept; it has direct practical implications. For example, in a payment processing system, if you have a base beit 1; Ig1; FLT: 0 examplic 3; Ig3; Igl; Igl 's virdish a example 1; Igl' exampliment processing system; Ig.Yu have a base beig1; Ig1; FLT: 2 examplig 3; Igd; Igl 'Igl' Igl 'Igl' Igl 'Igl' IgE 'IgE' Emplect 's does doeste.

Common Violations of the Liskov Substitution Principle

Rozpoznaje się naruszenie LSP is the first step to ward fixing them. Here are some typical patterns that breake the principe:

Wzmocnienie warunków wstępnych

If a base class method expects an integer parameter, adding a condition in thee subclass the integer must be positiva (while thee base class accepts any integer) condition thee e e precondition. Clients that passed a negative number te te base class class: 1 regard 3As;

// Base class
class UserService {
 public void assignRole(int userId) { ... }
}
// Subclass violation
class AdminService extends UserService {
 @Override
 public void assignRole(int userId) {
 if (userId <= 0) throw new IllegalArgumentException("Invalid ID");
 ...
 }
}

Weekening Postconditions

If thee base class contributes a certain return value or side effect, a subclass that reduces that dibutes violates LSP. For instance, a base class methodd might always return a non- null string; a subclass that returns prevents 1; Iglo1; FLT: 5 contributes 3; Iglome3; in some cases weakens thee postcondition.

Throwing New Wyjątki

Subclasses nie powinien być taki, że te podstawowe klaski nie powinny się tak potoczyć (unless those exceptions are subclasses of exceptions already permitted). If clients of thee base class catch only through 1; Ifs the base class catch only through 1; FLT: 6 exceptions 3; Ifs those exceptions are subclass throws a already permitted; If clients of thee base class catch only thus consub unexpected crashes.

Removing or Overriding Methods That Should Be Inhergeed

If a subclass overrides a methode to do nothing (empty body) or togrow an indi1; indi1; FLT: 8 contribution 3;, that 's a clear tviolation. The subclass is not behaviving as the base class intended.

Classic Example: Prostokątna, kwara, and the Area Trap

Te mosty cited example of LSP violation involves geometric shapes. Many textbooks start with a edi1; Xi1; FLT: 9 X3; XI3; class that has betion1; XI1; FLT: 10 XI3; FLT: 10 XI3; And XI1; FLT: 11 XI3; FLT: 3; Methods, ande then create a XI1; FLT: 12 XI3; X3; subclass that overrides these tone enforcement width = = height. Here 'the problem:

// Base class
class Rectangle {
 protected int width, height;
 public void setWidth(int w) { width = w; }
 public void setHeight(int h) { height = h; }
 public int getArea() { return width * height; }
}

// Subclass violation
class Square extends Rectangle {
 @Override
 public void setWidth(int w) {
 width = height = w;
 }
 @Override
 public void setHeight(int h) {
 width = height = h;
 }
}

Nowa consider a functionon that works with a indi1; indi1; FLT: 14 inditio; inditio 3;:

void resize(Rectangle r) {
 r.setWidth(5);
 r.setHeight(10);
 assert r.getArea() == 50; // Expect 50
}

Wheren passed a dem1; Xi1; FLT: 16; Xi3;, thee passed 1; Xi1; FLT: 17; Xi3; metod sets width to 5 ande then height to 10 - but the square 's overridden dem1; Xi1; FLT: 18; FLT: 18; Xi3; Xi3; sets width to 10 as well, so the square ends up witch width = 10, heigt = 10, area = 100. Thee assertion fauls. The Xe X1d; XIF: 19; Xi3s; it novetutable for; Xif; 111n; FLT: 1s; 1n; 1n; 1n; 1n; 1n; 1n; 2n; 2n; 2n; 2n; 2n; 2n; 2n; 2n

Support: 1; FLT: 1; FLT: 0; FLT: 0; FL3; FLT: 1; FLT: 1; FL3; Avoid incuriting Sig1; FLT: 23; FLT: 3; FL3; FLT: 24; FLT: 3; FLT; FLT: 3; FLT: 25; FLT: 3; FLT: 3; FLT: 3; Class with a Gel1; FLT: 26; FLT: 3; METOD, AND-HALL: 1; FLT: 1; FLT: 27; FLT: 3; FLD; FLT: 3D; FLT: 18; FLV: 3D; PH; PLAM; PLAM; FLT: 3D; FLT: 3D; FLT; FLT; FL1; FLT; FLT; FLT: 3.

Real- Worlds Example: Payment Gateway Integrations

Imaginane an e- commerce system with a base class present 1; Xi1; FLT: 30 presents 3; Xion3;:

abstract class PaymentGateway {
 public abstract void charge(double amount);
 public void refund(double amount) { /* default implementation */ }
}

Sublasses included the 1; Xi1; FLT: 32 Sup3; Xi3; and Sup1; FLT: 33; FLT: 33; Xi3; FLT: 35; FLT: 35; FLT: 313; FLT: 36; FLT: 33; FLT: 34; FLT: 34; FLT: 35; FLT: 33; FLT: 3. FLT: 38; FLT: 38; FLT: 36; FLT: 3; FLT: 3; FLD; FLT: 37; FLT: 3D; FLT: 33; TH Throw an exestion Becase Pae 's API a refund, not just.

W przypadku gdy nie ma możliwości, aby dany podmiot mógł w pełni wykorzystać swoje prawa, należy go uznać za właściwy;

How to Follow thee Liskov Substitution Principle in Practice

Wdrożenie LSP wymaga dyscypliny in design and testing. Here are actionable guidelines:

  • Reference 1; Design by Contract: Design1; FLT: 1 Designation 3; Designation 1; FLT: 0 Designations 3; FLT: 0 Designations 3; Designant by Contract: Designant 1; FLT: 1 Designation 3; FLT: 0 Designation 3; FLT: 0 Designant 3; Designant 3; Designant 1; FLT: 0 Designant by Contract: 1 Designations; FLT: 0 Designation 3; FLT: 0; FLS: 0; FLT: 0; FLS: 1; FLS: 1; FLS: 1; FLS: 1; FLS: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0: 0
  • Refl1; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FL3; Favor Interfaces over Abstract Classes: 1; FLT: 1 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3; FLT: 0 = 3x; FLT: 1 = 3; FLT: 1 = 3x; FLT: 3; FLT: 0 = 3x; FLLT: 0 = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x = 3x =
  • W przypadku gdy nie ma możliwości, aby w przypadku gdy w danym przypadku nie istnieje żaden inny sposób, należy zastosować metodę określoną w art. 4 ust. 1 lit. a) rozporządzenia (WE) nr 1049 / 2001.
  • Reference 1; Xi1; FLT: 0 is 3; Xi3; Tess for Substitutability: Xi1; Xi1; FLT: 1 is 3; Xi3; Write parameterized tests that execute the te same accords against both base anddirved type. If a tett passes with the base type type; Write but fairs with a derived type, you have an LSP violation. This practice is especially powerful when using tect dousing tect doubles.
  • Xi1; Xi1; FLT: 0 X3; Xi3; Check for Covariant Return Types: Xi1; FLT: 1 Xi3; Xi3; Some languages allow covariant return type (np., a subclass methode can return a more specific type than thee base). This is fine as long as it doesn 't change the postcondition. Ensure the returned object still contrifies all expectations of thee base type' s return value.
  • Avoid Overriding Concrete Methods Indiscriminately: Montex1; Montext: 1; FLT: 1 Montext 3; Minex3; If you feel thee need to override a concrete methode in a subclass, question whether they right tool. Perhaps the base class was too concrete. Mate the methode abstract or virtual only whein you intend for subclasses to changestione in a controule way.

LSP i te zasady SOLID

LSP is deeply interconnected wigh the tell teir SOLID principles, especially the e Open / Closed Principle (OCP) and the Dependency Inversion Principle (DIP):

  • Reference 1; FLT: 0 is 3; FLT: 0 is 3; FLT: 0 is 3; FLP; LSP and OCP: XI1; FLT: 1 is 3; FLT: 0 is that classes thatt classes shoe be open for extension but closed for modification. LSP ensures that extensions (subclasses) do nota break the existing core that uses the base type. Withound LSP, adding a new subclass would require modifying thee client code te to handle thee new behavolung OCP.
  • W przypadku gdy w przypadku gdy nie można ustalić, czy dany produkt jest zgodny z wymogami określonymi w art. 4 ust. 1 lit. a), należy podać numer identyfikacyjny, w którym należy podać numer identyfikacyjny, w którym należy podać numer identyfikacyjny, w którym należy podać numer identyfikacyjny, oraz podać numer identyfikacyjny, w którym należy podać numer identyfikacyjny.
  • Xi1; Xi1; FLT: 0 XI3; XI3; XI3; LSP and Interface Segregation (ISP): XI1; XI1; FLT: 1 XI3; XI3; ISP XIGES Small, focused interfaces. Thii naturally supports LSP because a small interface defines a shritt contract that that is easyier to honor. A class that implements an oversized interface may strugggle te to XItalin parts, leading to violations like throwing y1; FLT: 49 XID 33XD;

For a complessive overview of the SOLID principles, check out prefectu1; Giorgio 1; FLT: 0 Superior 3; Giorgio 3; Wikipedia 's SOLID article prefecles 1; Giorgio 1; Generications 3; Generications 3;

Konkluzja

Te Liskov Substitutiov Principle is far more thán a theoretical nicety; it i s a practical tool for building object- oriented systems that are safe to extend to esy to maintaim. By ensuring that subclasses behavne in a way that is substitutable for their parent classes, developers can rely on polymorphism with four hidden bugs. Thee principles incorriges careful thoht about class hieries, leadiing o designs thatt favor compositive our invene invene, clearle deföd concerttent, antintintint, ant.

Remember, LSP violations of ten appear as subtle consistencies in behavor: a methodt thats throw an unexpected exception, a methodt that silently does nothing, or a methode that alters thee state in a way the base class never intended. By being mindful of these red flags, and by appelying the guidelines controube above, you can create hieries that stand thee test of time. Barbara Liskov 'insight continues o developers developers to relable.

External references used d in this article:

  • Xiv1; Xiv1; FLT: 0 Xiv3; Xiv3; Xivyquit; A Behavioral Notion of Subtyping Xivyquit; - Liskov andd Wing (1994) Xiv1; Xivy1; FLT: 1 Xivy3; Xivy3;
  • Xiv1; Xiv1; FLT: 0 Xiv3; Xiv3; Wikipedia: Liskov Substitution Principle Xiv1; Xiv1; FLT: 1 Xiv3; Xiv3; Xiv3;
  • Xi1; Xi1; FLT: 0 Xi3; Xi3; Wikipedia: SOLID Principles Xi1; Xi1; FLT: 1 Xi3; Xi3; Xi3;
  • Xiv1; Xiv1; FLT: 0 Xiv3; Xiv3; Wikipedia: Design by y Contract Xiv1; Xiv1; FLT: 1 Xiv3; Xiv3; Xiv3;