Created
November 11, 2014 00:24
-
-
Save mehdi-farsi/345a371d8810a1f7e2b2 to your computer and use it in GitHub Desktop.
Example of "Composition Over Inheritance" Concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vehicle; end | |
class Car < Vehicle | |
def initialize | |
# A composition to separate standalone Engine class. | |
# That permit to create a class Bicycle without give it | |
# the property of an engine Vehicle. | |
@engine = GasolineEngine.new | |
end | |
def move_to_gasoline | |
end | |
end | |
class Engine; end | |
class GasolineEngine < Engine | |
def start | |
puts 'Gasoline Engine is started.' | |
end | |
def start | |
puts 'Gasoline Engine is stopped.' | |
end | |
end | |
class DieselEngine < Engine | |
def start | |
puts 'Diesel Engine is started.' | |
end | |
def start | |
puts 'Diesel Engine is stopped.' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment