Is there a design pattern I can use for a situation like below. This is an example I made up to try and explain the situation I'm dealing with currently. I keep having to add new DeviceScript classes to my DeviceAnalyzer as situation come up and adding logic about when to return the new type in the 'GetScriptForDevice'.
This example I have given is pretty simple but the business logic I'm having to implement for my real 'GetScriptForDevice' isn't always this easy. Any suggestions on a pattern or an example of a better way to structure this code to follow a better pattern would be greatly appreciated. I'm violating OCP every time I need to add a new DeviceScript and I'm trying to find a way around this because my class is getting quite long and fragile.
Thanks.
public class DeviceScript {
public string Name { get; set; }
public string SecurityScriptName { get; set; }
public string BusinessScriptName { get; set; }
}
public class DeviceAnalyzer {
public const string US_SecurityScript = "US_SecurityScript";
public const string US_BusinessScript = "US_BusinessScript";
public const string UK_SecurityScript = "UK_SecurityScript";
public const string UK_BusinessScript = "UK_BusinessScript";
private DeviceScript UnitedStates = new DeviceScript {
Name = "US_Script",
SecurityScriptName = US_SecurityScript,
BusinessScriptName = US_BusinessScript
};
private DeviceScript UnitedKingdom = new DeviceScript {
Name = "UK_Script",
SecurityScriptName = UK_SecurityScript,
BusinessScriptName = UK_BusinessScript
};
public DeviceScript GetScriptForDevice(IDevice device) {
if(device.Location == Locations.US) {
return UnitedStates;
}
if(device.Location == Locations.UK) {
return UnitedKingdom;
}
return null;
}
}