[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Object-oriented philosophy


Over the summer, I've been working on a simulation. After months
of design and redesign, I finally coded it up in two days over
Labor Day weekend. Works great.

The core of the simulation is a set of models of three different
types of electrical loads (characterized based on how they respond
to voltage changes), implemented as python classes.

Since the three classes all had common methods (by design), I
thought that maybe refactoring these three classes to inherit from
a parent class would be beneficial. I went ahead and did so.
(Outlines of before and after are at the end of the post.)

Net net is that the only thing that ended up being common was the
__init__ methods. Two of the classes have identical __init__
methods; the third has a superset of that method. The other methods
all have completely different implementations. This isn't due to
poor coding, but due to the fact that what these model have
different physical characteristics.

Not being that familiar with object-oriented programming (I grew up
on FORTRAN and c), I'm seeking opinions:

Is there really any benefit to this change? Yes, I've eliminated
some (a few lines per class) duplicate code. On the other hand,
I've added the parent class and the (probably small, but not
non-existent) overhead of invoking super().

How does one judge when it's worthwhile to do this and when it's
not? What criteria would somebody seasoned in OO and python use
to say "good idea" vs "don't waste your time"?

Other thoughts on this topic would also be appreciated.


Comparison follows:

============= begin original ================
class ConstantPower( ):
  def __init__( self, xmlmodel, V ):
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
class ConstantCurrent( ):
  def __init__( self, xmlmodel, V ):
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
class ConstantImpedance( ):
  def __init__( self, xmlmodel, V ):
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
  def Update( self, V ):
=============== end original ================

============= begin revised =================
class LoadModel( ):
  def __init__( self, xmlmodel, V, id ):
class ConstantPower( LoadModel ):
  def __init__( self, xmlmodel, V ):
    super().__init__( xmlmodel, V, "constant power" )
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
class ConstantCurrent( LoadModel ):
  def __init__( self, xmlmodel, V ):
    super().__init__( xmlmodel, V, "constant current" )
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
class ConstantImpedance( LoadModel ):
  def __init__( self, xmlmodel, V ):
    super().__init__( xmlmodel, V, self.name )
  def Resistance( self, V ):
  def Power( self, V ):
  def FixedValue( self ):
  def Update( self, V ):
=============== end revised =================

-- 
Michael F. Stemper
This email is to be read by its intended recipient only. Any other party
reading is required by the EULA to send me $500.00.