开发者

which one of those python implementations is better

which one of the following is considered better a design and why ?. i have 2 classes , one for the gui components and the other is for it's events.

please put in mind that the eventClass will be implemented so many times, (sometimes to get data from an oracle databases and sometimes mysql databases )

class MainWindow:

    def __init__(self):
        self.myEvents = eventClass()  # the class that has all 开发者_C百科the events 
        self.button = button # consider it a button from any gui library
        self.menu  = menu  # menu box 

    def bottonEvent(self):
        data = self.myEvents.buttonEvent()
        self.menu.populate(data)

class eventClass:

    def __init__(self):
          pass
    def getData(self):
         return data # return data to puplate in the list

OR

class MainWindow:

    def __init__(self):
        self.myEvents = eventClass(self)  # the class that has all the events 
        self.button = button # consider it a button from any gui library
        self.menu  = menu  # menu box 

    def bottonEvent(self):
        self.myEvents.ButtonEvent()

class eventClass:

    def __init__(self,window):
          pass
    def ButtonEvent(self):
         window.menu.populateData()

please inform me if anything was unclear

please help ,

thanks in advance


The first choice is better "decoupled": the event class needs and has no knowledge whatsoever about the window object or its menu attribute -- an excellent approach that makes the event class especially easy to unit-test in isolation without any overhead. This is especially nice if many implementations of the same interface need to exist, as you mention they do in your case.

The second choice introduces a mutual dependency -- an event object can't work without a window object, and a window object builds an event object. That may be an acceptable complication in more abstruse cases where it buys you something, but for this specific use it sounds more like an arbitrary extra difficulty without any real plus.

So, I would recommend the first form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜