1

I have a device which have low level programming. I am giving version numbers every new devices and upgrades. I also have a program which communicate with these devices (to retrieving information on these devices).

For eg. v1.2 sends this kind of string:

v1.2|Time|Conductivity|Repetation|Time|Heat of First Nozzle|Pressure|EndOfMessage

but new version of device program:

v1.3|Time|Conductivity|Repetation|Time|Humadity|1st Nozzle Heat;2nd Nozzle Heat|Pressure|EndOfMessage

My test application will retrieve information and change the operation of this device. Some operations will have in v1.2 device some not. I thought strategy design pattern seems useful for this situation but I'm not sure. Which design pattern should I use to do this?

uzay95
  • 16,052
  • 31
  • 116
  • 182

2 Answers2

5

Yes, this would be a good use-case for the Stategy pattern, although you will also use the Factory pattern to create a specific parser instance.

Your code should then generally look something like this:

public DeviceInfo Parse(InputData input)
{
    var version = versionParser.Parse(input);
    var concreteParser = parserFactory.CreateFor(version);
    var data = concreteParser.Parse(data);
    return data;
}

For a simple project with few parsers, you may hardcode your parser factory:

public class ParserFactory
{
    public static IParser<DeviceInfo> CreateFor(Version version)
    {
        // instantiate proper parser based on version
    }
}

Depending on the size of your project, you may also decide to use a plugin pattern for your parsers (System.AddIn contains useful classes for managing plugins).

vgru
  • 49,838
  • 16
  • 120
  • 201
  • That is a great answer. I would also point out at the less technical http://en.wikipedia.org/wiki/Facade_pattern – Josep Valls Mar 14 '12 at 12:32
0

I feel Strategy along with Factory method will solve the purpose.

Shailesh
  • 1,178
  • 11
  • 12