2016-06-10 56 views
1

我有一個表示請求的類。我有一組非常具體的請求,比如「I-95」,「P-22」等,它們執行不同的功能並由控制器類調用。這樣做的最好方法是什麼,以便人們可以輕鬆地添加更多請求?創建一組Python類

我有這樣的事情在此刻:

class Requests: 
    def __init__(self): 
      self.types = [ 
        'I-95', 
        'P-22', 
        ... 
      ] 

    def generate_request(self,type, Data): 
      # Here I would call the appropriate case for the type, e.g. P-22 

的請求的情況將是在它自己單獨的文件,並期待這樣的:

class P-22: 
    # Members 
    def __init__(self, Data): 
     # Set data members 
    def request(self): 
     # Submit request 

,我將能夠創建在控制器中的請求

f = Requests() 
f.generate_request('RC75') 

我正在努力做到乾淨,儘可能。謝謝!

+0

您對目前的解決方案有什麼不喜歡的? – rrauenza

+0

對於初學者來說,我想用類似於請求類型的列表來實現類似於enum的東西。鍵值對字典可能以請求的名稱作爲鍵和指向其功能的指針作爲其值。 – Answoquest

回答

8

試着這麼做:

class BaseRequest: 
    name = None 


class FooRequest(BaseRequest): 
    name = 'I-95' 

    def response(self): 
     return "foo" 


class BarRequest(BaseRequest): 
    name = 'P-22' 

    def response(self): 
     return "bar" 


class RequestManager: 

    def __init__(self): 
     self.requests = { 
      FooRequest.name: FooRequest, 
      BarRequest.name: BarRequest 
     } 

    def generate_request(self, name): 
     if name in self.requests: 
      return self.requests[name]() 

    def register_request(self, request_class): 
     assert issubclass(request_class, BaseRequest), \ 
      'Request class not a subclass of BaseRequest' 
     assert hasattr('name', request_class) and isinstance(request_class.name, str), \ 
      'Request name not correctly configured' 
     self.requests[request_class.name] = request_class 

然後:

manager = RequestManager() 
request = manager.generate_request('I-95') 
if request is not None: 
    print(request.response()) # "foo" 

而對於註冊新的要求:

class NewRequest(BaseRequest): 
    name = 'N-1' 

    def response(self): 
     return "new" 

manager = RequestManager() 
manager.register_request(NewRequest) 
request = manager.generate_request('N-1') 
if request is not None: 
    print(request.response()) # "new" 

我個人認爲這是一個使用了一個Singleton-pattern做得更好RequestManager(未經測試!):

class RequestManager: 

    instance = None 

    class __RequestManager: 
     requests = { 
      FooRequest.name: FooRequest, 
      BarRequest.name: BarRequest 
     } 

     def generate_request(self, name): 
      if name in self.requests: 
       return self.requests[name]() 

     def register_request(self, request_class): 
      assert issubclass(request_class, BaseRequest), \ 
       'Request class not a subclass of BaseRequest' 
      assert hasattr('name', request_class) and isinstance(request_class.name, str), \ 
       'Request name not correctly configured' 
      self.requests[request_class.name] = request_class 

    def __new__(cls): 
     if not cls.instance: 
      cls.instance = cls.__RequestManager() 
     return cls.instance 

    @staticmethod 
    def getInstance(): 
     return RequestManager() 

這將創建一個靜態訪問RequestManager例如:

manager = RequestManager.getInstance() 
# Rest same as before, register some requests, etc. 

manager2 = RequestManager.getInstance() # This is actually the same manager ie. the same instance! 

managermanager2共享相同的請求字典,所以更新通過其中一個適用於(從技術上來說,以同樣的經理,因爲您檢索相同的實例兩次)

+0

Beautiful,10/10,11/10 with rice – Answoquest

+0

另一個問題:我希望開發人員能夠編寫自己的案例文件,並將其添加到包含所有案例文件的包中。我怎麼能這樣做,而不必他們必須明確地添加新的文件到導入。我希望他們只需要添加文件並添加一個帶有名稱和函數指針的新行 – Answoquest

+1

允許註冊新請求的答案已更新 –