lundi 5 octobre 2020

How to implement Factory pattern with multiple parameters and each parameters are interface

I am a little bit confused on Factory Method with multiple parameters in which all parameters can change from GUI by user as seen below picture.

GUI Screen parameter selection

For each combobox item I have an interface and concrete implementations.

I have a SignalProcessor class which gets parameters as this 3 interfaces as below:

public interface ISignalProcessor
{
    double[] Process(double[] data);
}

 public class SignalProcessor : ISignalProcessor
{
    private IFft _fft;

    private IWindowing _windowing;

    private IInverseSpectrum _inverseSpectrum;

    private IDecimation _decimation;

    public SignalProcessor(IWindowing windowing, IFft fft,  IInverseSpectrum inverseSpectrum, IDecimation decimation)
    {
        _windowing = windowing;
        _fft = fft;
        _inverseSpectrum = inverseSpectrum;
        _decimation = decimation;
    }

    public double[] Process(double[] data)
    {
        var windowingResult = _windowing.Calculate(data);
        var fftResult = _fft.Calculate(windowingResult);
        var inverseSpectrumResult = _inverseSpectrum.Calculate(fftResult);
        return _decimation.Calculate(inverseSpectrumResult);
    }
}

I decided to produce and use concrete classes according to the selected combobox values so the following factory class created.

public static class FactorySP
{
    public static ISignalProcessor Create(string windowingType, int fftSize, bool isInverse, string decimationType)
    {
        return new SignalProcessor(CreateWindowing(windowingType), CreateFft(fftSize), CreateInverseSpectrum(isInverse), CreateDecimation(decimationType));
    }

    private static IWindowing CreateWindowing(string windowingType)
    {
        switch (windowingType)
        {
            case "Triangular":
                return new Triangular();
            case "Rectangular":
                return new Rectangular();
            case "Hanning":
                return new Hanning();
        }
    }

    private static IFft CreateFft(int fftSize)
    {
        switch (fftSize)
        {
            case 128:
                return new Fft128();
            case 256:
                return new Fft256();
            case 512:
                return new Fft512();
            default:
                return new FftNull();
        }
    }
    private static IInverseSpectrum CreateInverseSpectrum(bool isInverse)
    {
        if (isInverse)
            return new InverseSpectrumTrue();

        return new InverseSpectrumFalse();
    }

    private static IDecimation CreateDecimation(string decimationType)
    {
        if (decimationType == "RealTimeDecimation")
            return new RealTimeDecimation();

        return new SweepDecimation();
    }
}

Then used as follows:

_signalProcessor = FactorySP.Create(WindowingType, FftSize, InverseSpectrum, DecimationType);
result = _signalProcessor.Process(Enumerable.Range(0, 100).Select(a => (double)a).ToArray());

Is there a better way to get what I want than that? I feel there is something missing in the method I use :) I know Factory method is not like that but otherwise I have to create all combinations and permutations of overload of factory classes.

Please help me to improve class implementation.

Best Regars, Hakan

Aucun commentaire:

Enregistrer un commentaire