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

转载于:https://www.cnblogs.com/mcmurphy/archive/2012/11/07/3355624.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值