Created
April 22, 2019 17:18
-
-
Save omedale/a6d54e00efedf61b6f9fe384fabe379d to your computer and use it in GitHub Desktop.
Composition - OOP
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
| # when a class uses another object to provide some or all of its functionality | |
| class Movement | |
| def step | |
| puts "stepping" | |
| end | |
| def crawl | |
| puts "crawling" | |
| end | |
| end | |
| class Human | |
| def initialize | |
| @movement = Movement.new | |
| end | |
| def move | |
| @movement.step | |
| end | |
| end | |
| class Raccoon | |
| def initialize | |
| @movement = Movement.new | |
| end | |
| def move | |
| @movement.crawl | |
| end | |
| end | |
| human = Human.new | |
| raccoon = Raccoon.new | |
| human.move | |
| raccoon.move |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment