Looking for some OO design advice

Posted by Andrew Stephens on Programmers See other posts from Programmers or by Andrew Stephens
Published on 2012-10-15T15:04:31Z Indexed on 2012/10/15 15:52 UTC
Read the original article Hit count: 178

I'm developing an app that will be used to open and close valves in an industrial environment, and was thinking of something simple like this:-

public static void ValveController
{
    public static void OpenValve(string valveName)
    {
        // Implementation to open the valve
    }

    public static void CloseValve(string valveName)
    {
        // Implementation to close the valve
    }
}

(The implementation would write a few bytes of data to the serial port to control the valve - an "address" derived from the valve name, and either a "1" or "0" to open or close the valve).

Another dev asked whether we should instead create a separate class for each physical valve, of which there are dozens. I agree it would be nicer to write code like PlasmaValve.Open() rather than ValveController.OpenValve("plasma"), but is this overkill?

Also, I was wondering how best to tackle the design with a couple of hypothetical future requirements in mind:-

  1. We are asked to support a new type of valve requiring different values to open and close it (not 0 and 1).
  2. We are asked to support a valve that can be set to any position from 0-100, rather than simply "open" or "closed".

Normally I would use inheritance for this kind of thing, but I've recently started to get my head around "composition over inheritance" and wonder if there is a slicker solution to be had using composition?

© Programmers or respective owner

Related posts about design

Related posts about design-patterns