Created
November 27, 2011 16:39
-
-
Save mgreenly/1397781 to your computer and use it in GitHub Desktop.
Decorator Pattern using SimpleDelegator
This file contains 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
require 'delegate' | |
class Foo | |
attr_accessor :fname, :lname | |
def initialize(fname, lname) | |
@fname, @lname = fname, lname | |
end | |
end | |
# create a foo object | |
foo = Foo.new('john', 'smith') | |
class FooDecorator < SimpleDelegator | |
def full_name | |
"#{fname} #{lname}" | |
end | |
end | |
# create and use a decorated foo | |
decorated_foo = FooDecorator.new(foo) | |
puts decorated_foo.full_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment