• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

(翻译)C#中的SOLID原则–里氏替换原则

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#IEnumerable集合发布时间:2022-07-10
下一篇:
C#winform中自定义用户控件然后在页面中调用用户控件的事件发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap