3
\$\begingroup\$

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. enter image description here

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. Should I use the builder pattern to create the SignalProcessor object?

\$\endgroup\$
1
  • \$\begingroup\$ If you do it in MVVM way, the parameter source will be Property, not UI. \$\endgroup\$
    – aepot
    Commented Oct 7, 2020 at 18:41

1 Answer 1

2
\$\begingroup\$

If I understand your intent and code properly then you rather have an abstract factory implementation than a factory method pattern implementation.

The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition.

The Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

Reference 24th slide | Detailed discussion about the differences

Your FactoryFP exposes several methods to create multiple different objects (so called object family). Whereas a Factory Method's scope is a single object. And because of this an Abstract Factory can rely on multiple Factory Methods.


"Is there a better way to get what I want than that?"
As always it depends what do you want.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thank you that is what I am looking for. \$\endgroup\$
    – YrnHkn
    Commented Oct 9, 2020 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.