Created
February 28, 2012 21:34
-
-
Save jblanche/1935304 to your computer and use it in GitHub Desktop.
Inheritance vs 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
player1.is_better_than(player2) | |
# or | |
coach1.is_at_least_as_good_as(coach2) |
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
class Player | |
is_capable_of "comparison" | |
def compare(otherPlayer) | |
if this.time_spent_on_field > otherPlayer.time_spent_on_field { return 1 } | |
else if this.time_spent_on_field == otherPlayer.time_spent_on_field { return 0 } | |
else { return -1 } | |
end | |
end | |
class Coach | |
is_capable_of "comparison" | |
def average | |
this.points_earned / this.time_spent_on_bench | |
end | |
def compare(otherCoach) | |
if this.average > otherCoach.average {return 1} | |
else if this.average == otherCoach.average {return 0} | |
else {return -1} | |
end | |
end | |
module Comparison | |
def is_better_than(other) | |
return this.compare(other) == 1 | |
end | |
def is_worst_than(other) | |
return this.compare(other) == -1 | |
end | |
def is_equal_to(other) | |
return this.compare(other) == 0 | |
end | |
def is_at_least_as_good_as(other) | |
return this.compare(other) >= 0 | |
end | |
end |
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
def is_better_than(otherPerson) | |
if(this.kind == "Player") | |
# use criteria 1 | |
else if (this.kind == "Coach") | |
# use criteria 2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment