2011-10-18 41 views
1

我想写一个封装了逻辑类:连接类,静态或实例?

  • 构建基于另一个类的属性和主机/端口的信息从一个配置文件
  • 特定的URL进行连接
  • 解析响应

    class Foo(object): 
        def __init__(self, a, b): 
         self.a = a 
         self.b = b 
         self.connect_id = None 
         self.response = None 
    
        def something_that_requires_bar(self): 
         # call bar 
         pass 
    
        # ...other methods 
    

如果连接类是一堆staticmethods/classmeth的返回我正在查找的数据的ods?

class Bar(object): 
     def build_url(self, Foo): 
      # get host/port from config 
      # build url based on Foo's properties 
      return url 

     def connect(self, url): 
      # connects to the url that was built 
      return Bar.parse_response(the_response) 

     def parse_response(self, response): 
      # parses response 

我也应该构建认为我需要的,这样我可以在连接后,从中提取数据的数据的对象?

class Bar(object): 
     def __init__(self, foo): 
      self.url = 

     def _build_url(self): 
      # build url based on Foo's properties    
      self.url = # do something with Foo 

     def _parse_response(self, response): 
      # parses response 

     def connect(self, url): 
      # connects to the url that was built 
      self.raw_response = urllib.urlopen(self.url).read() 
      self.parsed_response = self._parse_response(self.raw_response) 

甚至混合?

class Bar(object): 
     def __init__(self, foo): 
      self.foo = foo 
      self.url = self._build_url() 

     def _build_url(self): 
      # build url based on Foo's properties    
      self.url = # do something with Foo 

     def _parse_response(self, response): 
      # parses response 

     @classmethod 
     def connect(cls, Foo): 
      # connects to the url that was built 
      bar = Bar(Foo) 
      self._build_url() 
      self.raw_response = urllib.urlopen(self.url).read() 
      self.parsed_response = self._parse_response(self.raw_response) 
      return bar 
+0

我会对类之间的循环依赖关系的设计保持警惕。也就是说,你有一个带'something_that_requires_bar'方法的类'Foo'和一个带'Foo'作为参数的类'Bar'。这意味着一个相当紧密的耦合(这不是一件好事)。 – Wilduck

+0

这是我的问题之一,这就是为什么我问......这只是我看起来不正确。 – sasker

回答

1

一个单独的类应该理想地代表一个明确的,有凝聚力的行为。有时,它并不总是很清楚最适合的是什么,但你的情况,我想你的每一个步骤的代码作为一个独特的类或函数是这样的:

def build_url(foo): 
    # build the url from foo 
    return the_url 

def get_response(url): 
    # do the connection, read the response 
    return the_response 

def parse_response(response): 
    # parse the response 
    return parsed_response 

response = get_response(build_url(foo_object)) 
stuff_you_want = parse_response(response) 

你可以做同样的事情类'而不是函数,如果任何这些步骤需要更多的内部逻辑,这将更好地在类构造中服务。例如它可能是有道理的URL和响应解析逻辑在类中:

class Url(object): 
    def __init__(self, foo): 
     self._url = self._build_url_from_foo(foo) 

    def _build_url_from_foo(self, foo): 
     # do the url stuff 
     return the_url 

    def url_string(self): 
     return self._url 

class ResponseWrapper(object): 
    def __init__(self, response): 
     self._response = response 

    def parse(self): 
     # parsing logic here 
     return parsed_response 

response = ResponseWrapper(get_response(Url(foo))) 
response.parse() 
+0

对......我的情况相当勉强,我不太确定采取哪种方法。 – sasker

0

嗯......以及为什么不把它分成两类;一个用于从配置中提取信息,另一个用于将该信息与来自类的信息结合起来执行您需要执行的任务。根据进入这些任务的内容,将事情进一步分解可能是有意义的。另外,正如Wilduck所指出的那样,从中获取信息的Foo的界面需要考虑......确保“组合器”不需要知道更多关于Foo实例的健康信息。

class Config(object): 
    ''' 
    read configuration from file 
    ''' 

    configurations = {} 
    def __new__(cls, config_file): 
     try: 
      return cls.configurations[ config_file ] 
     except KeyError: 
      return super(Config, cls).__new__(cls, config_file) 

    def __init__(self, config_file): 
     if getattr(self, 'config_read', False): 
      return 
     self.read_config(config_file) 
     self.config_read = True 

    def read_config(self): 
     ... 

class Combiner(object): 
    ''' 
    build url based on config and other object 
    ''' 

    def __init__(self, config_file, foo): 
     self.config = Config(config_file) 
     self.foo = foo 

    def build_url(self): 
     ... 

    def connect(self): 
     ... 

    def parse_response(self): 
     ... 
+0

我关心的不是配置阅读等等,而是像解析响应,连接url等东西似乎可以是属性?这就是为什么我不确定是使用静态方法还是使用方法分离类。 – sasker