How I implement Decorator Pattern in Ruby
I'd like to know how you guys implement decorator pattern. Here's how I do it.
Given this class:
class Sender
def talk
puts "Hello world!"
end
end
How do we add a logging mechanism in such a way that we will not alter the code of the above class?
This is how I do it:
- Create a new class.
class LogSender
def initialize(sender)
@sender = sender
end
def send
#log something here
@sender.talk
end
end
Written by Mark
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#