Created
March 4, 2013 18:03
-
-
Save blackwatertepes/5084161 to your computer and use it in GitHub Desktop.
Composition
This file contains hidden or 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
Inheritance Model | |
class Car | |
attr_reader :color, :wheels, :cost | |
def initialize(args) | |
@wheels = 4 | |
@color = args[:color] | |
@cost = :respectible | |
end | |
end | |
class Ferarri < Car | |
def initialize(args) | |
super(args) | |
@cost = :expensive | |
end | |
end | |
class Yugo < Car | |
def initialize(args) | |
super(args) | |
@cost = :dirt | |
end | |
end | |
Composition Model | |
class Luxury | |
attr_reader :color, :wheels, :cost | |
def initialize(args) | |
@wheels = 4 | |
@color = args[:color] | |
@cost = args[:cost] | |
end | |
end | |
class Ferarri | |
attr_reader :luxury | |
def initialize(args) | |
@luxury = Luxury.new({color: args[:color], cost: args[:cost]}) | |
end | |
end | |
class Yugo | |
attr_reader :luxury | |
def initialize(args) | |
@luxury = Luxury.new({color: args[:color], cost: args[:cost]}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment