Skip to content

Instantly share code, notes, and snippets.

@stevecass
Last active August 29, 2015 14:10
Show Gist options
  • Save stevecass/9b555430e2bddb2004e5 to your computer and use it in GitHub Desktop.
Save stevecass/9b555430e2bddb2004e5 to your computer and use it in GitHub Desktop.
Composition and delegation
# WheelType knows about the behavior of wheels
class WheelType
def diameter
#snip
end
def rim_size
#snip
end
def weight
#snip
end
end
class Bicycle
attr_accessor :wheel_type
def initialize options = {}
#do some stuff
self.wheel_type = WheelType.new options
end
def rim_size
#delegate to wheel_type
wheel_type.rim_size
end
# add other delegate methods for
# wheel weight diameter, behavior
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment