博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Understanding Liskov Substitution
阅读量:6983 次
发布时间:2019-06-27

本文共 3570 字,大约阅读时间需要 11 分钟。

Introduction

Here I am going to discuss Liskov substitution principle of SOLID.

Background

If you read my previous two articles it will be better for you to understand SRP,OCP,ISP,DIP of SOLID.

Using the Code

Before start technical discussion I want to answer the below questions:

What is Liskov Substitution principle?

Answer:"Objects  in a program should be replaceable with their subtypes without alerting the correctness of that program"

Is my answer tough to understand? OK,make it easier.

"It means that we must make sure that new derived classes are extending the base classes without changing their behavior".

Let's consider an example to make it better for understanding.Suppose I buy a computer(desktop) from a Computer Shop after view desktop and laptop. Now  I ask for desktop screen size. Let's convert my requirements into code.

using System;namespace LiskovSubstitution{    public class Computer    {        public string GetComputerDiscripution()        {            return "You get a desktop";        }        public string GetColor()        {            return "Color is white";        }        public virtual string GetScreen()        {            return "LCD 19 inch";        }    }    public class Laptop:Computer    {        public new string GetComputerDiscripution()        {            return "You get a laptop";        }        public string GetColor()        {            return "Color is black";        }        public override string GetScreen()        {            return "LED 19 inch";        }    }    class Program    {        static void Main(string[] args)        {            Computer desktop = new Laptop();            desktop.GetComputerDiscripution();//You get a desktop            desktop.GetColor();//Color is white            desktop.GetScreen();//LED 19 inch--------------violation liskov        }    }}

What "LED 19 inch"? It should be "LCD 19 inch"(violation of LSP).

Now I told the shop manager that your promotion shows that desktop computer have LCD screen.Please give me the right product. So how to do it in code?

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LiskovSubstitutionObey{    public abstract class Computer    {        public abstract string GetComputerDescription();        public abstract string GetColor();        public abstract string GetScreen();            }    public class Desktop:Computer    {        public override string GetComputerDescription()        {            return "You get a desktop";        }        public override string GetColor()        {            return "Color is white";        }        public override string GetScreen()        {            return "LCD 19 inch";        }    }    public class Laptop:Computer     {        public override string GetComputerDescription()        {            return "You get a laptop";        }        public override string GetColor()        {            return "Color is black";        }        public override string GetScreen()        {            return "LED 19 inch";        }    }    class Program    {        static void Main(string[] args)        {            Computer computer = new Laptop();            computer.GetComputerDescription();//You get a laptop            computer.GetScreen();//LED 19 inch        }    }}

Yes finally I get it.

So we can say that the new derived classes are extending the base classes without changing their behavior or objects  in a program should be replaceable with their subtypes' without alerting the correctness of that program".So our code satisfy LSP rule.

 

转载地址:http://wvvpl.baihongyu.com/

你可能感兴趣的文章
微信 登录 Scope 参数错误或没有 Scope 权限
查看>>
C# 温故知新 基础篇(7) 接口<思维导图>
查看>>
jQuery Makes Parsing XML Easy[转]
查看>>
CSS里常见的块级元素和行内元素
查看>>
Windows Azure Storage (4) Windows Azure Storage Service存储服务之Blob Share Access Signature
查看>>
framework调试
查看>>
java线程(2)--同步和锁
查看>>
Rafy 框架 - 大批量导入实体
查看>>
go1
查看>>
使用 Palette 让你的 UI 色彩与内容更贴合
查看>>
关于ASP.NET"未能映射路径"问题
查看>>
详谈如何定制自己的博客园皮肤
查看>>
iBATIS配置文件的特殊使用方法
查看>>
Python正则表达式指南
查看>>
T-SQL 根据年月日创建DateTime
查看>>
【CSS进阶】CSS 颜色体系详解
查看>>
论:CMMI项目策划方法(PP)
查看>>
高可用高性能分布式文件系统FastDFS实践Java程序
查看>>
【Coursera课程笔记】Web智能和大数据Week3_MapReduce
查看>>
从头写个http client(java)
查看>>