Design pattern
Commandモードはコマンドをカプセル化し、コマンド発行者の責任とコマンド実行者の責任を分離します.
rorのデータ移行は彼を使うことです
rorのデータ移行は彼を使うことです
- class Command
- attr_reader :description
- def initialize(description)
- @description = description
- end
- def execute
- raise "wrong"
- end
- end
-
- class CreateFile < Command
- def initialize(file_name)
- super("create file : #{file_name}")
- end
- def execute
- puts "create file"
- end
- def unexecute
- puts "deleting created file"
- end
- end
- class CopyFile < Command
- def initialize(file_name)
- super ("copy file : #{file_name}")
- end
- def execute
- puts "copy file"
- end
- def unexecute
- puts "deleting copied file"
- end
- end
-
- class CompositedCommand < Command
- def initialize
- @commands = []
- end
- def add_command(cmd)
- @commands << cmd
- end
- def execute
- @commands.each { |cmd| cmd.execute }
- end
- def unexecute
- @commands.each { |cmd| cmd.unexecute }
- end
-
- def description
- description = ""
- @commands.each { |cmd| description += cmd.description + "
"}
- description
- end
- end
-
- cmds = CompositedCommand.new
- cmds.add_command(CreateFile.new("file")) #
- cmds.add_command(CopyFile.new("file"))
- puts cmds.description
-
- cmds.execute