Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created March 10, 2012 02:34
Show Gist options
  • Save takaheraw/2009769 to your computer and use it in GitHub Desktop.
Save takaheraw/2009769 to your computer and use it in GitHub Desktop.
class Command
attr_reader :description
def initialize(description)
@description = description
end
def execute
end
end
class CompositeCommand < Command
def initialize
@commands = []
end
def add_command(cmd)
@commands << cmd
end
def execute
@commands.each {|cmd| cmd.execute}
end
def description
description = ''
@commands.each {|cmd| description += cmd.description + "\n"}
description
end
end
class CreateFile < Command
def initialize(path, contents)
super("Create file: #{path}")
@path = path
@contents = contents
end
def execute
f = File.open(@path, "w")
f.write(@contents)
f.close
end
end
class DeleteFile < Command
def initialize(path)
super("Delete file: #{path}")
@path = path
end
def execute
File.delete(@path)
end
end
cmd = CompositeCommand.new
cmd.add_command(CreateFile.new("test.txt","hoge"))
cmd.add_command(DeleteFile.new("test.txt"))
cmd.execute
puts cmd.description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment