LSP It's the only design principle named after a person , And the author is also a “ Female doctor ” 

=============================================================


LSP,Liskov substitution principle, Chinese translation for “ Richter's principle of substitution ”.

This is the only one of the object-oriented principles named after people , Even though Liskov There is no popularity in China UNIX The great masters of the world (Kenneth Thompson、Dennis Ritchie)、GOF The gang of four is so loud , But check the information , You'll find that in fact Liskov It's also very good :2008 Turing prize winner in , The first female computer doctor in history . Its specific information can be found on Wikipedia :http://en.wikipedia.org/wiki/Barbara_Liskov

Get down to business , Let's see LSP What are the principles .

LSP The original explanation, of course, comes from Liskov Lady, it's time to , She is in 1987 Year of OOPSLA It was proposed at the meeting that LSP principle ,1988 year , She published her article in ACM Of SIGPLAN Notices The journal , It's explained in detail LSP principle :

A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra.What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

English is relatively long , I look tired , Let's simply translate and summarize :

1)  The object of the subclass provides all the behavior of the parent class , And add something extra to the subclass ( Can be a function , It can also be attributes );

2)  When a program is implemented based on a parent class , Suppose the subclass is replaced by the parent class and the program does not need to be changed , It means that it conforms to LSP principle

Although we've translated and sorted it out a little bit , But it's still very awkward and hard to understand .

Fortunately, there is also Martin The master also thinks this is not very easy to understand ,Robert Martin stay 1996 Years for 《C++ Reporter》 I wrote an article entitled 《The The Liskov Substitution Principle》 The article , Explain, for example, the following :

Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.

Translation is : When a function uses a pointer or reference to a parent class , Must be able to use subclass objects without knowing the subclass type .

Martin The Master explained , relative easy More understanding . but Martin The master is not satisfied yet , stay 2002 year ,Martin In his published 《Agile   Software   Development   Principles   Patterns   and   Practices》 In a Book , It is further simplified as :

Subtypes   must   be   substitutable   for   their   base   types.

Translation is : Subclasses must be replaceable with their parents .

after Martin Master's two translations , I Believe LSP The principle itself has been explained quite well easy I understand , But the point is : How to satisfy LSP principle ? Or more generally : When can subclass replace parent class ?

We know , For the caller (Liskov What's mentioned in the explanation P), Interacting with a parent class is nothing more than two parts : Calls a method of the parent class 、 Get the output of the parent class method , The intermediate process ,P I don't know .

in other words , The relationship between the caller and the parent class is now two-sided : Function input , Output function . For example, the following figure :

With this picture , How to do LSP The principle is clear :

1)  The subclass must implement or inherit all the public functions of the parent class , Otherwise, the caller calls a function in a parent class , And there's nothing in the subclass , There will be errors when executing ;

2)  The input parameters of each function of a subclass must be the same as the parent class , Otherwise, the code that calls the parent class cannot call the child class ;

3)  The output of each function of the subclass ( Return value 、 Change global variables 、 Insert database 、 Sending network data, etc ) Must be no less than the parent class , Otherwise, the processing based on the output of the parent class cannot be completed .

With these three principles , It is very convenient to infer whether the class design conforms to LSP Principle . It's important to note that 3 The key to this is “ No less than the parent class ”, That is to say, it can be more than the parent class , namely : The output of the parent class is a subset of the output of the subclass .

Some friends may be a little puzzled when they see these three principles : These three principles come out one by one , What's the difference between that subclass , It's all the same , There will be different subclasses ?

In fact, if we study these three principles carefully , You'll find that It's just a convention on input and output , And there's no constraint in the middle of the process . such as : The output of the same write database ,A Class can be read XML The data is then written to the database ,B Class can read data from other databases and then use the local database ,C Class can get data by analyzing the business log and then write it to the database . this 3 The process of each class is different , But it's all written to the database in the end .

LSP The classic example of a principle is “ Rectangles and squares ” This sample . From a mathematical point of view , A square is a special rectangle , But from an object-oriented perspective , Square is not a subclass of rectangle . The reason is that for rectangles , After setting the width and height , area  =  wide  *  high ; But for a square , Set the height and set the width at the same time , Setting the width sets the height at the same time , The final area is not equal to what we set   wide  *  high , It's equal to the square of the last set width or height .

The specific code sample ratio is as follows :

Rectangle.java

package com.oo.java.principles.lsp;
/**
* Rectangle
*/
public class Rectangle { protected int _width;
protected int _height; /**
* Set the width
* @param width
*/
public void setWidth(int width){
this._width = width;
} /**
* Set high
* @param height
*/
public void setHeight(int height){
this._height = height;
} /**
* Get the area
* @return
*/
public int getArea(){
return this._width * this._height;
}
}

Square.java

package com.oo.java.principles.lsp;
/**
* Square
*/
public class Square extends Rectangle { /**
* Set up “ wide ”, Unlike a rectangle : Set the width of the square , The height of the square is set at the same time
*/
public void setWidth(int width){
this._width = width;
this._height = width;
} /**
* Set up “ high ”, Unlike a rectangle : Set the height of the square , The width of the square is set at the same time
*/
public void setHeight(int height){
this._width = height;
this._height = height;
} }

UnitTester.java

package com.oo.java.principles.lsp;
public class UnitTester {
public static void main(String[] args){
Rectangle rectangle = new Rectangle();
rectangle.setWidth(4);
rectangle.setHeight(5); // For example, the following assert Infer as true
assert( rectangle.getArea() == 20); rectangle = new Square();
rectangle.setWidth(4);
rectangle.setHeight(5); // For example, the following assert Infer as false, Assertion failed , Throw out java.lang.AssertionError
assert( rectangle.getArea() == 20);
}
}

At the same time, the above example also gives an inference about whether the subclass conforms to LSP It's a clever way to , namely : Unit test cases for parent classes , Whether the incoming subclass can also pass the test . Suppose the test passes , It means that it conforms to LSP principle , Otherwise, it means that it does not meet the requirements LSP principle

Serial : Object oriented sunflower classic : thought 、 Skills and practice (32) - LSP More articles on principles

  1. Serial : Object oriented sunflower classic : thought 、 Skills and practice (28) - Design principles : cohesion & coupling

    In front, we explained the object-oriented development process of one link by one through examples : Use case model -> Domain model -> The design model ( Class model + dynamic model ), The problem of how to do object-oriented is solved . Now we're going to talk about " How to face ...

  2. IT Twentieth days - Object oriented programming idea 、 abstract class 、 exception handling 、 Program operation log record 、 This week's summary ***

    IT Twentieth days In the morning Object oriented programming idea 1. Assemble the design of the computer : (1) The composition of the computer : Monitor + The case (2) The composition of the chassis : Power Supply + a main board + Hard disk (3) The components that the motherboard contains :cpu+ Memory +PCI Interface +usb Interface (4)PC ...

  3. Black horse programmer ——OC Basic grammar of language Object-oriented thinking

    Java train .Android train .iOS train ..Net train . Looking forward to communicating with you ! ( The following is a summary of the personal knowledge of the entrance video of black horse apple )( One ) Basic grammar 1) keyword @interface.@implementati ...

  4. Java Object-oriented thinking

    Object-oriented thinking 1. The characteristics of object-oriented thinking : ① In line with people's daily thinking habits ② Can simplify complex problems ③ Change the former executor into the current commander Object-oriented thinking , The so-called object , It's actually the entity . The description of the object , There are usually two sides ...

  5. Object oriented programming idea (OOP)

    In this paper, I will discuss how to solve all kinds of difficult problems in software development from the perspective of object-oriented programming , To talk about our understanding of object-oriented programming ideas , Sort out the four basic characteristics of object-oriented . Seven design principles and 23 The relationship between two design patterns . Difficult problems in software development : The software is complex ...

  6. U3D Aircraft battle (MVC Pattern ) analysis -- Object oriented programming idea

    In their own research U3D In the game , I've seen some people's simple game development videos , Well written , It's just that individuals are java web Developer , So combined with MVC thought , There's a lot of thinking about game development . If we can make the game more ideological , Division of labor , Development will tell ...

  7. OC - 1. The contrast between process oriented and object-oriented ideas

    One . Process oriented 1> thought Process oriented is a process centered programming idea , Object oriented features are not supported . Process oriented is a modular programming method 2> development method The process oriented development method is based on the process ( It can also be called module ) ...

  8. .net The custom of JS Control , Use of the Object-oriented thinking encapsulation 了 Control (.net The first day of custom control development )

    Hello everyone ! My name is Liu Jing , I'm glad you can see the article I shared ! I hope it can help you ! Let's start with a few examples , You can see how Custom control ! Business needs : Make   One   Belong to Their own     Button object    , then image ...

  9. Object oriented programming idea ( Prequel )-- You have to know javascript

    In writing object-oriented programming ideas - In the design pattern js Part of the time found a lot of basic knowledge do not understand the words , It's hard to really understand and read js Object oriented code . So , Here's a quick fill . And then we continue with our idea of object-oriented programming - Design patterns . What is duck type ...

Random recommendation

  1. say something Angular Medium $timeOut Timer

      It's very unfortunate that , People often seem to AngularJS Medium $timeOut() Function as a built-in . Don't worry about functions . however , If you forget $timeOut() The callback function of will cause very bad effect , You may encounter ...

  2. InternalsVisibleToAttribute—— hold internal Member is exposed to the specified friend assembly

    Introduction to friend assemblies We know that a class is defined as internal Members of ( Including the type . Method . attribute . Variable . event ) Can only be accessed in the same assembly ( Yes, of course , I'm talking about the normal way , Not including access through reflection ). The rule is . ...

  3. Object Properties of property Explain in detail ( Automatically generate member variables )

    class Class Properties in property: stay ios In the first edition , We declare both properties and underlying instance variables for the output , At that time , The attribute is oc A new mechanism of language , And you have to declare the corresponding instance variable , for example : @interfac ...

  4. Study KnockOut The first one is Hello World

    Study KnockOut The first one is Hello World I just started to learn KnockOut. The content is equivalent to a study note . And pay homage to the official website here , I prefer their home Live Example Block , There are jsFiddl ...

  5. Netty Realize long connection communication and heartbeat detection between server and client

    adopt netty Realize long connection communication between server and client , And heart rate detection .        The basic idea :netty The server uses a Map Save all connected clients SocketChannel, Client's Id As Map Of key. Every time ...

  6. GitHub Open source that can be used on

    AGi18n :https://github.com/angelolloqui/AGi18n It's easy to localize your iOS app, From the code and XIB Extract text from the file and convert it into a localizable string , And it won't change XIB writing ...

  7. Windows Remove the desktop SVN File or folder question mark

    The version Library Check out the content of To desktop , Only later did I find the files on my desktop It's all a question mark , I didn't think it was a big problem , Delete .svn that will do , But delete all .svn after , There are still question marks on the desktop , It's been refreshed many times , And restart the computer 了 , The question mark didn't disappear ...

  8. Python Of Tkinter Put the window at the top

    root = Tk()root.wm_attributes('-topmost',1)

  9. take eclipse The structure of the directory on the left is changed to A tree structure

    In case of recognition Eclipse The display of is as follows : This view doesn't look very convenient for us , So we need to change the display structure , Let's click on the icon circled in blue in the figure below Put the mouse in the pop-up box "package prese ...

  10. netstream Play rtmp Live stream Caton

    The standard flash player ActionScript3 sentence , Play flash publish Of rtmp flow ,NetConnection--->NetStream--->play--->atta ...