Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Created November 11, 2014 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/345a371d8810a1f7e2b2 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/345a371d8810a1f7e2b2 to your computer and use it in GitHub Desktop.
Example of "Composition Over Inheritance" Concept
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