What this principle states is that “in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program”. The principle was first introduced by Barbara Liskov.
The principle imposes some requirements that are:
• Contravariance of method arguments in the subtype.
• Covariance of return types in the subtype.
• No new exceptions should be thrown by methods of the subtype, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertype (wikipedia)
Also I have to say that in the 11th video of the clean code they talk about this principle and say important things like:
• The representative rule: it says that representatives don’t share the relationships of the things they represent.
• Inheritance brings rigidity
• One of the possible solutions in problems with violation to the Liskov´s Substitution principle could be apply the adapter pattern.
Moreover there are some heuristics:
• If the base class do something the derive class must do it too.
• Violation: A derive function is written to unconditional throws an exception.
• Violation: The presence of “if instance of” statement.
As an example I’m going to reproduce the example showed by Uncle Bob in the video of clean code. The example consists of a program that uses the class rectangle but then there is a new requirement that makes implement the square class, and as all of we know a square is a type of rectangle. But what happens is that sometimes we don’t know what type of object will be returned.
//Violation
class Rectangle
{
protected int width;
protected int height;
public void setWidth(int width){
width = width;
}
public void setHeight(int height){
height = height;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public int getArea(){
return width * height;
}
}
class Square extends Rectangle
{
public void setWidth(int width){
width = width;
height = width;
}
public void setHeight(int height){
width = height;
height = height;
}
}
/*Applying the Liskov´s substitution principle, what we can do is making a class square that does not extend the rectangle class. And we can do that because as Uncle Bob says a square is a type of rectangle but the class rectangle is not a rectangle is a bunch of methods that represents the behavior of a rectangle.*/
class Rectangle
{
protected int m_width;
protected int m_height;
public void setWidth(int width){
m_width = width;
}
public void setHeight(int height){
m_height = height;
}
public int getWidth(){
return m_width;
}
public int getHeight(){
return m_height;
}
public int getArea(){
return m_width * m_height;
}
public int getPerimeter(){
return (2 *m_width ) + (2 * m_height);
}
}
class Square
{
protected int side;
public void setSide(int side){
side = side;
}
public void getSide(){
return side;
}
public int getArea(){
return side * side;
}
public int getPerimeter(){
return 4* side;
}
}
Bibliography
wikipedia. (n.d.). wikipedia.org. Retrieved august 11, 2013, from http://en.wikipedia.org/wiki/Liskov_substitution_principle
No hay comentarios:
Publicar un comentario