Object oriented design with Javacasting and a derived class with more member variables than the base

I have a base class, let's call it DispenseResult that stores the result of a machine operation. I am designing for a family of machines. DispenseResult will contain member variables that are applicable to all machines, such as current drawn during an operation.

I was thinking of having a derived class to model certain specialized machines, e.g. MotorizedDispenseResult which would have member variables pertaining to motor function etc.

A part of the client code is to get these various member variables from DispenseResult (or a derived class) and log them. If I do it the way described above then I'll end up with code like:

void log(DispenseResult result) {
  // store results common to all machines
  log("current = " + result.getCurrent());

  // store results for specialized cases
  if (result instance of MotorizedDispenseResult) {
    log("error = " + ((MotorizedDispenseResult)result).getMotorError());
    ... etc.

This is going to get messy as new derived types are created with all the instanceof checking. Is there a cleaner way to do it?

TY, Fred

4 Answers
  1. Option 1

    1. create a method on the base class, possibly abstract.
    2. have the subclasses implement the method.
    3. where you are doing the type check, call the method instead.

    That way all the subclass specific deviations live in the subclasses instead of somewhere else.

    Option 2

    If the functionality needs to live outside the class hierarchy, What I would do is

    1. define an interface 'Handler' with a method 'execute'. Name according to what it is doing in your app.
    2. Create an implementation for each subclass.
    3. Create a Map<SpecificClass, Handler>
    4. Where you are doing the if-statement on type, lookup the appropriate handler in the map and call execute.

    This why you don't have to write extensive if statements, you can let the map do it for you. This is also easier to test, you can test your Handler impls in isolation.

    2011-02-26 14:59:49
  2. In OO Design, you would add the behaviour to the class, and simply override it in subclasses to provide more specialized behaviour.


    class DispenseResult
         String log()
             return "current = " + getCurrent();
     class MotorizedDispenseResult extends DispenseResult
          String log()
               String s = super.log();
               s += "error = " + getMotorError();
               return s;
    2011-02-26 18:08:11
  3. It depends on what you need from this class. In your example, it seems that you only want a specific error message. You can just add a method getError to the super class, so subclasses have to override it.

    2011-02-26 15:02:44
  4. provide a polymorphic method print() in DispenseResult class. Printing themselves is the responsibility of these family of objects anyway.

    Pangea2011-02-26 15:00:48
Related Articles
You Might Also Like