tdudzik

SOLID - Liskov Substitution Principle

Apr 11th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. /* Liskov Substitution Principle */
  2.  
  3. // Before
  4. class Rectangle {
  5.  
  6.     protected int width;
  7.     protected int height;
  8.  
  9.     public Rectangle(int width, int height) {
  10.         this.width = width;
  11.         this.height = height;
  12.     }
  13.  
  14.     public int getWidth() {
  15.         return width;
  16.     }
  17.  
  18.     public void setWidth(int width) {
  19.         this.width = width;
  20.     }
  21.  
  22.     public int getHeight() {
  23.         return height;
  24.     }
  25.  
  26.     public void setHeight(int height) {
  27.         this.height = height;
  28.     }
  29.  
  30.     public int area() {
  31.         return width * height;
  32.     }
  33.  
  34. }
  35.  
  36. class Square extends Rectangle {
  37.  
  38.     public Square(int side) {
  39.         super(side, side);
  40.     }
  41.  
  42. }
  43.  
  44. class App {
  45.  
  46.     public static void main(String[] args) {
  47.         Rectangle rectangle = new Rectangle(20, 40);
  48.         Square square = new Square(20);
  49.  
  50.         rectangle.area(); // 800
  51.         square.area(); // 400
  52.  
  53.         modifyRectangle(rectangle);
  54.         modifyRectangle(square);
  55.  
  56.         rectangle.area(); // 1000
  57.         square.area(); // 1000 instead of 2500
  58.     }
  59.  
  60.     static void modifyRectangle(Rectangle rectangle) {
  61.         rectangle.setHeight(50);
  62.     }
  63.  
  64. }
  65.  
  66. // After
  67. class Rectangle {
  68.  
  69.     private int width;
  70.     private int height;
  71.  
  72.     public Rectangle(int width, int height) {
  73.         this.width = width;
  74.         this.height = height;
  75.     }
  76.  
  77.     public int getWidth() {
  78.         return width;
  79.     }
  80.  
  81.     public void setWidth(int width) {
  82.         this.width = width;
  83.     }
  84.  
  85.     public int getHeight() {
  86.         return height;
  87.     }
  88.  
  89.     public void setHeight(int height) {
  90.         this.height = height;
  91.     }
  92.  
  93.     public int area() {
  94.         return width * height;
  95.     }
  96.  
  97. }
  98.  
  99. class Square {
  100.    
  101.     private int side;
  102.  
  103.     public Square(int side) {
  104.         this.side = side;
  105.     }
  106.  
  107.     public int getSide() {
  108.         return side;
  109.     }
  110.  
  111.     public void setSide(int side) {
  112.         this.side = side;
  113.     }
  114.  
  115.     public int area() {
  116.         return side * side;
  117.     }
  118.  
  119. }
  120.  
  121. class App {
  122.  
  123.     public static void main(String[] args) {
  124.         Rectangle rectangle = new Rectangle(20, 40);
  125.         Square square = new Square(20);
  126.  
  127.         rectangle.area(); // 800
  128.         square.area(); // 400
  129.  
  130.         modifyRectangle(rectangle);
  131.         modifySquare(square);
  132.  
  133.         rectangle.area(); // 1000
  134.         square.area(); // 2500
  135.     }
  136.  
  137.     static void modifyRectangle(Rectangle rectangle) {
  138.         rectangle.setHeight(50);
  139.     }
  140.  
  141.     static void modifySquare(Square square) {
  142.         square.setSide(50);
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment