python - Implement same methods in different classes -


i don't know how word problem, i'll try explain example.

let's have 3 gui classes:

  1. base surface class
  2. detailed surface class
  3. sprite class

all of them independent of each other, no inheritance among them. have function "drag()" makes surface/sprite dragable, , want implement function method 3 of them.

since it's exact same code implementations find annoying, cumbersome , bad practice rewrite code. thing came far make saperate class , inherit class. doesn't seem way go.

i'd thankfull advice.

edit

another example different setup - have following classes:

  1. basesurface
  2. dragable
  3. resizable
  4. eventhandler

only first 1 independent, others depend on first (must inherited). end user should, without effort, able choose between simple basesurface, 1 implements dragable, 1 resizable, 1 eventhandler, , combination. "without effort" mean end user should not have make e custom class , inherit desired classes plus call appropriate methods (init, update, ...) classes share.

so make class every possible combination, eg. "basesurfacedrag", "basesurfacedragresize", ... messy quickly. whats different , better approach this?

this kind of case should use parent class for. in both cases looks parent class (logically) should like:

class drawable(object):     def drag(self, *args, **kwargs):         """drag , drop behavior"""         # code goes here 

then each of other classes inherits that

class basesurface(drawable):     # stuff  class detailedsurface(drawable):     # stuff  class sprite(drawable):     # stuff 

in second case have interfaces, logically like:

class draginterface(object):     """implements `drag` method"""     def drag(self):         """drag , drop behavior"""         # code goes here  class resizeinterface(object):     """implements `resize` method"""     def resize(self):         """drag , drop resize"""         # code  class eventhandlerinterface(object):     """handles events"""     def handle(self, evt):         # code  class mynewsurface(basesurface, draginterface, resizeinterface):     """draggable, resizeable surface"""     # implement here 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -