我正在开发一个游戏应用程序(移动前端,Rails后端),并试图决定是否应该严格使用RESTful。看起来,如果我这样做,我将创建更多的控制器。例如,有几个游戏动作我需要实现,如攻击,防御等。如果我严格使用RESTful,我将需要为每个游戏动作创建一个控制器,只有一个REST动作(更新)。如果我不使用RESTul并创建一个通用的战斗控制器,那么我就可以创建用于攻击、防御等的方法/动作。严格意义上的RESTful似乎更麻烦。
任何真知灼见都将不胜感激。
发布于 2010-10-25 14:29:44
攻击、防御等都是同一种资源:Action。
例如:
PUT actions/attack # to attack
PUT actions/defend # to defend
GET actions # to get the list of all available actions要将其实现为REST,我应该这样做:
class PlayerActionsController ...
def index
@actions = PlayerAction.all
respond_with @actions
end
def update
@action = PlayerAction.find(params[:id])
respond_with @action.perform(params)
end
end
class GenericAction
attr_readable :name
def initialize(name)
@name = name
end
def perform(arguments)
self.send(name, arguments) if self.class.find(name)
end
ACTIONS = []
ACTIONS_BY_NAME = {}
class << self
def add_action(*names)
names.each do |name|
action = Action.new(name)
ACTIONS_BY_NAME[name] = action
ACTIONS << action
end
end
def index
ACTIONS.dup
end
def find(name)
ACTIONS_BY_NAME[name]
end
end
def
class PlayerAction < GenericAction
add_action :attack, :defend
def attack(params)
player, target = Player.find(params[:player_id]), Player.find(params[:target_id])
...
end
def defend(params)
...
end
end这只是给出一个如何做好它的粗略想法。
https://stackoverflow.com/questions/4012205
复制相似问题