如何在python中设计一个类(oo模式帮助)

时间:2014-07-10 20:55:44

标签: python oop

我正在实施一个"引擎"类(下面详细描述),我不确定使用什么类型的对象。我尝试过对OO模式进行一些阅读,但我还是不确定。我认为我的问题是与语言无关,但我确实使用了Python。

我想创建一个初始化的类(例如数据库连接和其他一些配置),然后可以重复调用它来处理信息位。对于一些信息,我将我的逻辑分解为一堆方法,但现在我对每个方法都有一个巨大的调用签名,因为我需要将各种事物传递给每个方法。

调用代码看起来像这样:

db = get_a_db_connection()
my_engine = Engine(db, config)
while True:
  my_info = get_info_from_somewhere()
  my_engine.process_info(my_info)

我所拥有的实际Engine类看起来像这样:

class Engine(object):

  def __init__(self, db, config):
    self.db = db
    # Also store the config - it's a little more complicated than
    # this but I am abstracting away details that don't seem needed
    self.config = config

  def process_info(self, info):
    foo = self.method1(info)
    bar = self.method2(info, foo)
    baz = self.method3(info, bar)
    qux = self.method4(info, foo, bar, baz) 
    bla = self.method5(info, bar, baz, qux)

  def method1(self, info):
    # Do something and return intermediate info
    return some_transformation_on_info

  # Definitions for method2 - method5 (and more) follow

  def method2(self, info, foo):
    ...

  <snip>

能够将这些中间事物存储为属性似乎很好,所以我不需要每次都将它们作为参数传递。但将它们存储为属性似乎并不合适,因为它们特定于一条信息,而不是整个类。

这是我使用工厂模式创建实际处理信息的中间对象的情况吗?

3 个答案:

答案 0 :(得分:3)

这在很大程度上取决于参数的逻辑。但是您可以考虑定义一个具有两个属性的绑定对象:引擎和信息。然后在绑定对象上移动所有这些函数。

答案 1 :(得分:0)

你可以传递一个环境:

class Engine(object):

  def __init__(self, db, config):
    self.db = db
    self.config = config

  def process_info(self, info):
    env = {'info': info}
    self.method1(env)
    self.method2(env)
    self.method3(env)
    self.method4(env)
    self.method5(env)

  def method1(self, env):
    env['foo'] = some_transformation_on_info(env['info'])

  def method2(self, env):
    env['bar'] = something_from(env['foo'])

  def method3(self, env):
    env['baz'] = my_func(env['bar'])

  def method4(self, env):
    env['qux'] = your_func(env['foo'], env['bar'], env['baz'])

  def method5(self, env):
    env['bla'] = your_func(env['bar'], env['baz'], env['qux'])

这样,process_info()并不需要知道它调用的所有方法中发生了什么。

如果您不喜欢字典订阅语法,可以执行以下操作:

class Environment(object):
    pass

class Engine(object):

  def __init__(self, db, config):
    self.db = db
    self.config = config

  def process_info(self, info):
    env = Environment()
    env.info = info
    self.method1(env)
    self.method2(env)
    self.method3(env)
    self.method4(env)
    self.method5(env)

  def method1(self, env):
    env.foo = some_transformation_on_info(env.info)

  def method2(self, env):
    env.bar = something_from(env.foo)

  def method3(self, env):
    env.baz = my_func(env.bar)

  def method4(self, env):
    env.qux = your_func(env.foo, env.bar, env.baz)

  def method5(self, env):
    env.bla = your_func(env.bar, env.baz, env.qux)

答案 2 :(得分:-1)

class EngineInfoProcessor(object):

    def __init__(self, engine, info):
        self.engine = engine
        self.info = info

    def process(self):
        foo = self.engine.method1(info)
        self.foo = foo # if you want
        yield foo # if you want
        bar = self.method2(info, foo)
        self.bar = bar # ...
        yield bar # ...
        baz = self.method3(info, bar)
        self.baz = baz
        yield baz
        qux = self.method4(info, foo, bar, baz) 
        self.qux = qux
        yield qux
        bla = self.method5(info, bar, baz, qux)
        self.bla = bla
        yield bla

可能就是你想要的。