Skip to content

Instantly share code, notes, and snippets.

@jblanche
Created February 28, 2012 21:34
Show Gist options
  • Save jblanche/1935304 to your computer and use it in GitHub Desktop.
Save jblanche/1935304 to your computer and use it in GitHub Desktop.
Inheritance vs Composition
player1.is_better_than(player2)
# or
coach1.is_at_least_as_good_as(coach2)
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
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