命令组合设计模式 [英] Composite of Commands Design Pattern
本文介绍了命令组合设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
灵感来自一般想法和这个博客文章中的示例模式实现,这里是一个刺激它可能是什么样的:
class CompositeCommand
def initialize(description,command,undo)
@ description = description; @命令=命令; @ undo = undo
@children = []
end
def add_child(child); @children<<<儿童;自; end
def execute
@ command.call()if @command&&& @ command.is_a?(Proc)
@ children.each {| child | child.execute}
end
def undo
@ children.reverse.each {| child | child.undo}
@ undo.call()if @undo&&& @ undo.is_a?(Proc)
end
end
使用软件安装程序的应用程序:
class CreateFiles< CompositeCommand
def initialize(name)
cmd = Proc.new {putsOK:#{name} files created}
undo = Proc.new {putsOK:#{name}文件已删除}
super(创建#{name}文件,cmd,撤消)
end
end
class SoftwareInstaller
def initialize; @命令= []; end
def add_command(cmd); @commands<<< CMD;自;结束
def install; @ commands.each(安培;:执行);自;结束
def卸载; @ commands.reverse.each(安培;:撤消);自身
end
安装程序= SoftwareInstaller.new
installer.add_command(
CreateFiles.new('Binary')。add_child(
CreateFiles.new ('Library'))add_child(
CreateFiles.new('Executable')))
installer.add_command(
CreateFiles.new('Settings')。add_child(
CreateFiles.new('Configuration'))add_child(
CreateFiles.new('Preferences'))。add_child(
CreateFiles.new('Help')))
installer.install# =>递归运行所有命令
installer.uninstall
Does anyone have a good example in Ruby of using a Composite of Commands? It is a design pattern hybrid that I have seen mentioned in various Design Patterns literature which sounds quite powerful, but have not been able to find any interesting use cases or code.
解决方案
Inspired by the general idea and the sample pattern implementations in this blog post, here's a stab at what it might look like:
class CompositeCommand
def initialize(description, command, undo)
@description=description; @command=command; @undo=undo
@children = []
end
def add_child(child); @children << child; self; end
def execute
@command.call() if @command && @command.is_a?(Proc)
@children.each {|child| child.execute}
end
def undo
@children.reverse.each {|child| child.undo}
@undo.call() if @undo && @undo.is_a?(Proc)
end
end
And sample usage using the application of a software installer program:
class CreateFiles < CompositeCommand
def initialize(name)
cmd = Proc.new { puts "OK: #{name} files created" }
undo = Proc.new { puts "OK: #{name} files removed" }
super("Creating #{name} Files", cmd, undo)
end
end
class SoftwareInstaller
def initialize; @commands=[]; end
def add_command(cmd); @commands << cmd; self; end
def install; @commands.each(&:execute); self; end
def uninstall; @commands.reverse.each(&:undo); self end
end
installer = SoftwareInstaller.new
installer.add_command(
CreateFiles.new('Binary').add_child(
CreateFiles.new('Library')).add_child(
CreateFiles.new('Executable')))
installer.add_command(
CreateFiles.new('Settings').add_child(
CreateFiles.new('Configuration')).add_child(
CreateFiles.new('Preferences')).add_child(
CreateFiles.new('Help')))
installer.install # => Runs all commands recursively
installer.uninstall
这篇关于命令组合设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文