[Tutor] OO refactoring trial ??

Chinook chinook.nr at tds.net
Tue Jun 28 08:16:59 CEST 2005

OO refactoring trial
Following is a simple trial structure of a refactoring (top-down to OO) 
learning exercise I'm doing.  Whether you call it a Factory pattern, COR 
pattern, or some hinze 57, I don't know what class to use till run time and 
I'm trying to avoid a lengthy "if" sequence, the test sequence is important, 
and to avoid code duplication I'll be using code objects in the "doit" 

You've already given me many good ideas in previous threads and this is where 
it got you :~)  This works, but would you please tell me:
1) What you don't like about the approach
2) The implications of using this in a recursive approach (referenced from 
but outside the recursive function) and if the decorators help with such.  
3) Any other comments you might offer

Thank you,
Lee C

=========== ootest.py 
class MF(object):
  def findit(t):
    for it in MF.__subclasses__():
      if it.testit(t):
        return it().doit
class A(MF):
  def testit(tv):
    if tv == 'relates to A':
      return True
      return False
  def doit(self):
    print '# did A #'
class B(MF):
  def testit(tv):
    if tv == 'relates to B':
      return True
      return False
  def doit(self):
    print '# did B #'
mydoit = MF.findit('relates to B')

mydoit = MF.findit('relates to A')

======== Test run ==============
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)]
Type "help", "copyright", "credits" or "license" for more information.
>>> import ootest
# did B #
# did A #

More information about the Tutor mailing list