Created
March 10, 2012 02:34
-
-
Save takaheraw/2009769 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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