[Tutor] class design - base classes with optional properties?

Marcus Goldfish magoldfish at gmail.com
Thu Jan 22 21:04:45 CET 2009


I'm trying to design a base class for a hierarchy.  The properties I want to
specify for the base class depend on the values of other properties of the
base class.  For instance, in this toy example of a base FoodProcessor
class:
class FoodProcessor:
    "Model for a kitchen appliance food processor"
    speed_settings     # example [1, 2, 3, 4]
    blade_settings      # example ["slice", "grate", "grind"]
    slice_thickness     # example ["thin", "thick"], but only if
blade_setting is "slice"

it only make sense to have the slice_thickness property set when
blade_settings = "slice"; otherwise, it would be preferable to only define
the speed_settings and blade_settings properties.  For example:

class ProcessorA(FoodProcessor):
    "A slicing-only processor"
    speed_settings = [1, 2, 3, 4]
    blade_settings = "slice"
    slice_thickness = ["thin", "thick"]

class ProcessorB(FoodProcessor):
    "A non-slicing processor"
    speed_settings = [1,2,3,4]
    blade_settings = ["grate", "grind"]
    slice_thickness = None

Can anyone suggest some work-arounds, or refactoring for this type of
design?  Is there a design pattern for this?

Thanks!
Marcus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090122/a3c54c25/attachment-0001.htm>


More information about the Tutor mailing list