Created
August 3, 2012 23:59
-
-
Save mbriggs/3252831 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
| 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