programming-examples/ruby/Basics/liskov_substitution.rb
2019-11-18 14:44:36 +01:00

83 lines
1.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Liskovs principle tends to be the most difficult to understand. The principle
# states that you should be able to replace any instances of a parent class
# with an instance of one of its children without creating any unexpected or
# incorrect behaviors.
class Rectangle
def initialize(height, width)
@height = height
@width = width
end
def set_height(height)
@height = height
end
def set_width(width)
@width = width
end
def square
@width * @height
end
end
# Solution
# LSP says is if we know the interface of Rectangle, We need to be able to guess
# the interface of subtype class Square
# Square.new(3).square => 9
class Square < Rectangle
def initialize(side)
super(side, side)
end
def set_height(height)
super(height)
@width = height
end
def set_width(width)
super(width)
@height = width
end
end
# Another common instance of a Liskov violation is raising an exception for an
# overridden method in a child class. Its also not uncommon to see methods
# overridden with modified method signatures causing branching on type in
# classes that depend on objects of the parents type. All of these either
# lead to unstable code or unnecessary and ugly branching.
class Animal
def walk
'walking_as_animal'
end
end
class Cat < Animal
def run
'run_as_cat'
end
end
# Solution
class Animal
def walk
'walking_as_animal'
end
def run
raise NotImplementedError
end
end
class Cat < Animal
def run
'run_as_cat'
end
end