zoukankan      html  css  js  c++  java
  • Understanding Liskov Substitution

    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.

  • 相关阅读:
    滑动窗口与选择搜索
    R-CNN,SPP-NET, Fast-R-CNN,Faster-R-CNN, YOLO, SSD, R-FCN系列深度学习检测方法梳理
    GD和SGD区别
    AlexNet、VGG、NIN、GoogLeNet、ResNet
    目标检测中的precision,recall,AP,mAP计算详解
    转:图像分类、物体检测、物体分割、实例分割、语义分割
    卷积网络CNN中各种常见卷积过程
    卷积网络中的通道(Channel)和特征图
    人工智能之卷积神经网络(CNN)
    科技文献检索(一)——课程介绍及信息素养
  • 原文地址:https://www.cnblogs.com/DebugLZQ/p/3065341.html
Copyright © 2011-2022 走看看