简体   繁体   English

如何组织此代码?

[英]How do I organize this code?

I'm trying to sort functions into their appropriate classes by turning them into methods (it is possible to leave a function outside of the class if it seems appropriate).我试图通过将函数转换为方法来将函数分类到适当的类中(如果合适,可以将函数留在类之外)。 But I'm not sure what to do with this particular function (shown below).但我不确定如何处理这个特定的功能(如下所示)。

First, here is my class:首先,这是我的课:

class OreBlobAction:
   def __init__(self, entity, image_store):
      self.entity = entity
      self.image_store = image_store

   def ore_blob_action(self, world, action, ticks):
      entity = self.entity
      entity_pt = entities.get_position(self.entity)
      vein = find_nearest(world, entity_pt, entities.Vein)
      (tiles, found) = blob_to_vein(world, self.entity, vein)

      next_time = ticks + entities.get_rate(entity)
      if found:
         quake = create_quake(world, tiles[0], ticks, self.image_store)
         worldmodel.add_entity(world, quake)
         next_time = ticks + entities.get_rate(entity) * 2

      schedule_action(world, self.entity,
         OreBlobAction(self.entity, self.image_store), next_time)

      return tiles

And now, here is the function:现在,这是函数:

def take_action(world, action, ticks):
   entities.remove_pending_action(action.entity, action)
   if isinstance(action, VeinAction):
      return vein_action(world, action, ticks)
   elif isinstance(action, MinerNotFullAction):
      return miner_not_full_action(world, action, ticks)
   elif isinstance(action, MinerFullAction):
      return miner_full_action(world, action, ticks)
   elif isinstance(action, OreBlobAction):
      return ore_blob_action(world, action, ticks)
   elif isinstance(action, OreTransformAction):
      return ore_transform_action(world, action, ticks)
   elif isinstance(action, EntityDeathAction):
      return entity_death_action(world, action, ticks)
   elif isinstance(action, WyvernSpawnAction):
      return wyvern_spawn_action(world, action, ticks)
   elif isinstance(action, WyvernAction):
      return wyvern_action(world, action, ticks)
   elif isinstance(action, VeinSpawnAction):
      return vein_spawn_action(world, action, ticks)
   elif isinstance(action, AnimationAction):
      return animation_action(world, action, ticks)

   return []

As you can see, this function accounts for the actions of not only the OreBlobAction class, but also multiple others.如您所见,此函数不仅考虑了 OreBlobAction 类的操作,还考虑了多个其他类的操作。 Would it be better to leave this function outside of the OreBlobAction class?将此函数留在 OreBlobAction 类之外会更好吗? Or is there a better way to do this?或者有没有更好的方法来做到这一点?

NOTE: If I leave this function out of the OreBlobAction class, and I try to run the program, I get this error:注意:如果我将此函数排除在 OreBlobAction 类之外,并尝试运行该程序,则会出现以下错误:

NameError: global name 'ore_blob_action' is not defined

Your big switch statement on the type of the action is a red flag for refactoring.你关于动作类型的大 switch 语句是重构的危险信号。 Is there anything to stop you moving the "take action" method into the action class itself?有什么可以阻止您将“采取行动”方法转移到行动类本身中吗? For instance例如

class Action(object):
    """ An action that can be performed on the game world. """

    def perform(self, world, ticks):
        """ Perform the action. """
        raise NotImplementedError()

This would be your base action class, then within each type of action, you would override the perform(...) method, for example这将是您的基本操作类,然后在每种类型的操作中,您将覆盖perform(...)方法,例如

class WyvernSpawnAction(Action):
    """ An action that spawns a Wyvern. """

    [... Some action specific initialisation code here ...]

    def perform(self, world, ticks):
        """ Spawn a Wyvern. """
        world.spawn(Wyvern(...))

Your boilerplate to remove the action from the world would remain, and you can now freely add new types of action without eventually adding hundreds more comparisons within your function.从世界中删除动作的样板将保留,您现在可以自由添加新类型的动作,而无需最终在您的函数中添加数百个比较。 In addition, you can now handle cases where actions can inherit the behaviour of other actions without having to be extremely careful about the ordering of your comparisons.此外,您现在可以处理操作可以继承其他操作的行为的情况,而不必非常小心比较的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM