Skip to content

Instantly share code, notes, and snippets.

@mbriggs
Created August 3, 2012 23:59
Show Gist options
  • Select an option

  • Save mbriggs/3252831 to your computer and use it in GitHub Desktop.

Select an option

Save mbriggs/3252831 to your computer and use it in GitHub Desktop.
composition
class CodeMonkey
attr_reader :programming_language
def initialize(language)
@programming_language = language
end
def program!
programming_language.do_your_magic!
end
end
CodeMonkey.new( Java.new ).program!
# benefit of this over inheritance is that the relationship (protocol) between the
# two objects is obvious. With inheritance, the child can override any bits of the
# parent, and tracing any given code path tends to get a lot harder (in non trivial
# cases)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment