Code Blocks as Commands : Command « Design Patterns « Ruby






Code Blocks as Commands


class Commander
  attr_accessor :command

  def initialize(&block)
    @command = block
  end

  def on_button_push
    @command.call if @command
  end
end

new_button = Commander.new do
   puts "asdf"
end

 








Related examples in the same category

1.An Easier Way of Command Pattern
2.Commands That Record (Command and composite pattern)
3.Undo by a Command