(翻译)C#中的SOLID原则 – 里氏替换原则
The SOLID Principles in C# – Liskov Substitution
原文地址:http://www.remondo.net/solid-principles-csharp-liskov-substitution/
The third post in the SOLID by Example series deals with the Liskov Substitution Principle (LSP). It states that derived classes must be substitutable for their base classes. The principle was first mentioned by Barbara Liskov in the late eighties.
SOLID示例代码的第三篇文章我们来关注里氏替换原则(LSP)。里氏替换原则是指父类出现的地方应该可以由其子类进行替换。该原则在八十年代末期由Barbara Liskov首次提出。
Most developers embrace the Liskov Substitution Principle without even knowing it. It’s quite obvious. If you take a look at the examples that break the principle, you’ll most likely find them to be a little odd. A common example is that of a rectangle and it’s derived class; square. Now a square is-a kind of rectangle, but can a rectangle be a substitution for a square?
显而易见的是,大多数开发者其实在不知不觉已经应用了该准则。如果你看到了违背该准则的样例,会发现它们看上去有那么一点奇怪。一个常规的示例是矩形及其派生类:正方形。矩形出现的地方能够被正方形替换吗?
namespace LiskovSubstitutionPrinciple { public class Rectangle { public virtual int Width { get; set; } public virtual int Height { get; set; } } public class Square : Rectangle { public override int Height { get { return base.Height; } set { SetWidthAndHeight(value); } } public override int Width { get { return base.Width; } set { SetWidthAndHeight(value); } } // Both sides of a square are equal. private void SetWidthAndHeight(int value) { base.Height = value; base.Width = value; } } }
The Liskov Substitution Principle is broken here, because the behavior of the Width and Height properties changed in the descendant class. Substitution of the Square with a Rectangle gives some unexpected results.
在上述代码中,里氏替换原则被打破了。因为矩形中长宽属性的行为在其派生类中发生了变化。这里,用正方形替换矩形会出现意料之外的结果。
using System; namespace LiskovSubstitutionPrinciple { internal class Program { private static void Main() { Rectangle rectangle = new Square(); rectangle.Width = 3; rectangle.Height = 2; Console.WriteLine("{0} x {1}", rectangle.Width, rectangle.Height); Console.ReadLine(); } } }
The Width of the substituted ‘Rectangle’ is 2 and not the given 3. Since the behavior setting the Square property is different from the Rectangle, we can’t substitute the Square for a Rectangle. A better design, following the Liskov Substitution Principle, has a base class Shape for both the Rectangle and Square descendant classes.
从运行结果可以看到,被正方形替换后矩形的宽度是2而不是给定的3。因为在正方形中,其属性的行为和矩形是不同的,当然就不能在矩形出现的地方用正方形替换。一个更好的遵循里氏替换原则的设计是,为正方形和矩形设置一个共同的“Shape”基类。
The Liskov Substitution Principle held here, because the behavior of the Width and Height properties were not changed in the descendant class. Substitution of the Square or Rectangle with the Shape class gives the expected result.
上面的设计就遵循了里氏替换原则。因为在子类中并没有改变宽度和高度属性的行为。用正方形和矩形替换Shape类会出现预期的结果。
using System; namespace LiskovSubstitutionPrinciple { internal class Program { private static void Main() { Shape rectangle = new Rectangle(); rectangle.Width = 3; rectangle.Height = 2; Shape square = new Square(); square.Width = 3; square.Height = 2; Console.WriteLine("Rectangle {0} x {1}", rectangle.Width, rectangle.Height); Console.WriteLine("Square {0} x {1}", square.Width, square.Height); Console.ReadLine(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述