• (翻译)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();
            }
        }
    }

  • 相关阅读:
    输入框的提示文字的样式设置
    javascript高级程序设计第3版——第12章 DOM2与DOM3
    javascript高级程序设计第3版——第1Java章 DOM扩展
    javascript高级程序设计第3版——第10章 DOM
    javascript高级程序设计第3版——第8章 BOM(浏览器对象模型)
    javascript高级程序设计第3版——第7章 函数表达式
    javascript高级程序设计第3版——第6章 面向对象的程序设计
    小程序上传图片
    微信小程序验证码/密码输入框,一个框一个数字
    php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
  • 原文地址:https://www.cnblogs.com/mcmurphy/p/3355624.html
Copyright © 2020-2023  润新知