1

非常に具体的なタスクを実行したいので、非常に単純なラックベースのアプリケーションを作成しています。

は次のserver.rbようになります。

Path= File.expand_path("#{File.dirname __FILE__}/../../")

require "bundler/setup"
require "thin"
require "rack"

%w(parser auth controller).each do |file| 
  require "#{Path}/app/server/#{file}.rb"
end


builder = Rack::Builder.app do
  use Auth
  run Parser.new    
end

Rack::Handler::Thin.run(builder, :Port => 8080, :threaded => true)

parser.rb次のようになります。

class Parser

  def initialize
    @controller = Controller.new
  end

  def call(env)
    req = Rack::Request.new(env).params
    res = Rack::Response.new
    res['Content-Type'] = "text/plain"

    command= req[:command]

    if command =~ /\A(register|r|subscribe|s)\z/i
      @controller.register     
    end


    res.write command
    res.finish 

  end
end

ここでの私の質問は、設計の見通しから、 のインスタンスを 1 つ作成Controllerして各リクエストで使用する方が良いですか (上記のコードの Idid のように)、またはリクエストごとに新しいコントローラーインスタンスを作成する方が良いですか (に変更@controller.register) Controller.new.register? どちらを使用するのが良いですか、そしてその理由は何ですか?

前もって感謝します

4

1 に答える 1