"デザインパターン" タグがついている記事

  • 2008/04/01 18:44

    Rubyでデザインパターン -Command-

    最近Rubyを勉強してます(遅いって?)。Rubyのソースを見る度に、なぜか受け付け難い雰囲気を感じていたのですが、最近会う人会う人がRuby(on Rails)で開発しているよ的な発言をされているので、やむにやまれず触発された感じです。

    言語学習はやっぱりコード書かなきゃと思いましたので、ここに晒す次第です。あまりデザインパターンに意味はありません。

    クラス図

    CommandExecutor.rb
    class CommandExecutor
    def initialize
    @commands = Array.new
    end
    def setCommand cmd
    @commands << cmd
    end
    def execute
    @commands.each do |cmd|
    cmd.execute
    end
    end
    end

    AbstractCommand.rb
    class AbstractCommand
    def initialize
    @option = ”;
    @command = ”;
    end
    def execute
    %x{@command};
    end
    def setOption opt
    @option = opt
    end
    end

    CurrentDirCommand.rb
    require ‘AbstractCommand.rb’
    class CurrentDirCommand < AbstractCommand
    def initialize
    @command = 'pwd';
    end
    def execute
    STDOUT << %x{#{@command}}
    end
    end

    ListCommand.rb
    require ‘AbstractCommand.rb’
    class ListCommand < AbstractCommand
    def initialize
    @command = 'ls';
    end
    def execute
    if @option.nil? then
    cmd = @command
    else
    cmd = @command [...]