I have a desktop application for manage a PLC variables. For read/write on the PLC I use a static class called PLCService
public static class PLCService
{
private Plc _plc;
public void WriteConfiguration(Configuration config)
{
////something to do
}
public int[] GetWeightTestValues()
{
return _plc.Read()....
}
}
Now, the problem is: we want to use this code also for a new machine. This newer machine changes just a little bit from the original machine and I have to add methods and change some methods implementations of the static class. At the end, I want to use this application for Machine A and for Machine B. When the application starts, application go to INI file, take the machine model and PLCService need to use the correct methods implementations.
My first idea was to introduce a property in the static class like this:
public static class PLCService
{
internal static SizerPLC Client;
public static ProgramMode ProgramMode { get; private set; }
public static void Init(ProgramMode programMode)
{
ProgramMode = programMode;
}
public static void Connect(string ipAddress)
{
if (ProgramMode == ProgramMode.Angurie)
Client = new SizerAnguriePLC(ipAddress);
else if (ProgramMode == ProgramMode.WagonSizer)
Client = new WagonSizerPLC(ipAddress);
}
but I don't like this solution and I'm sure is not the correct solution because I need to re-implements ALL the static class methods, and also the code begin redundant:
public static class PLCService
{
internal static SizerPLC Client;
public static ProgramMode ProgramMode { get; private set; }
public static int[] ReadWeightTestValues()
{
SizerPLc.ReadWeightTestValues();
}
Do you have any solution? I don't have a lot of experience with softwares development.
Your solution: Introducing properties does solve the problem, but leads to redundant code.
Use abstraction and inheritance. You can create an abstract base class or interface to define the common functionality of PLCService, and use this base class or interface to create two concrete implementations, each representing a different machine model.
create an interface: IPLCService
public interface IPLCService
{
void WriteConfiguration(Configuration config);
int[] GetWeightTestValues();
}
Implement this interface for each machine model: SizerAnguriePLC 和WagonSizerPLC
public class SizerAnguriePLC : IPLCService
{
private Plc _plc;
public void WriteConfiguration(Configuration config)
{
////something to do
}
public int[] GetWeightTestValues()
{
return _plc.Read()....
}
}
public class WagonSizerPLC : IPLCService
{
private Plc _plc;
public void WriteConfiguration(Configuration config)
{
////something to do
}
public int[] GetWeightTestValues()
{
return _plc.Read()....
}
}
Complete the machine model to instantiate the corresponding implementation and assign it to a variable of type IPLCService. You can access methods of IPLCService interface through this variable.
public static class PLCService
{
private static IPLCService _plcService;
public static void Init(ProgramMode programMode,string ipAddress)
{
if (programMode == ProgramMode.Angurie)
_plcService = new SizerAnguriePLC(ipAddress);
else if (programMode == ProgramMode.WagonSizer)
_plcService = new WagonSizerPLC(ipAddress);
}
public static int[] ReadWeightTestValues()
{
return _plcService.GetWeightTestValues();
}
}
To make changes based on this code, you can refer to abstraction and inheritance.