-1

I got a sample code based on switch statement. I want some how add it in arrayList or HashMap. Also, what do you think which option would be good to use and why?

public static final IFeatureExtraction create(final int piFeatureExtractionMethod, IPreprocessing poPreprocessing)
throws FeatureExtractionException
{
    IFeatureExtraction oFeatureExtraction = null;

    switch(piFeatureExtractionMethod)
    {
        case MARF.LPC:
            oFeatureExtraction = new LPC(poPreprocessing);
            break;

        case MARF.FFT:
            oFeatureExtraction = new FFT(poPreprocessing);
            break;

        case MARF.F0:
            oFeatureExtraction = new F0(poPreprocessing);
            break;

        case MARF.SEGMENTATION:
            oFeatureExtraction = new Segmentation(poPreprocessing);
            break;

        case MARF.CEPSTRAL:
            oFeatureExtraction = new Cepstral(poPreprocessing);
            break;

        case MARF.RANDOM_FEATURE_EXTRACTION:
            oFeatureExtraction = new RandomFeatureExtraction(poPreprocessing);
            break;

        case MARF.MIN_MAX_AMPLITUDES:
            oFeatureExtraction = new MinMaxAmplitudes(poPreprocessing);
            break;

        case MARF.FEATURE_EXTRACTION_PLUGIN:
        {
            try
            {
                oFeatureExtraction = (IFeatureExtraction)MARF.getFeatureExtractionPluginClass().newInstance();
                oFeatureExtraction.setPreprocessing(poPreprocessing);
            }
            catch(Exception e)
            {
                throw new FeatureExtractionException(e.getMessage(), e);
            }

            break;
        }

        case MARF.FEATURE_EXTRACTION_AGGREGATOR:
        {
            oFeatureExtraction = new FeatureExtractionAggregator(poPreprocessing);
            break;
        }

        default:
        {
            throw new FeatureExtractionException
            (
                "Unknown feature extraction method: " + piFeatureExtractionMethod
            );
        }
    }

    return oFeatureExtraction;
}

Do you think can any other pattern be applied to it. Mention the pattern name you recommend. Will appreciate extra implementation ideas.

user3651267
  • 83
  • 1
  • 10

1 Answers1

0

Create an interface that returns an IFeatureExtraction instance. Add instances of that interface to a HashMap using the Integer value as the key of the Map. Rather than invent your own interface, you could use an existing interface like javax.inject.Provider.

public interface IFeatureExtractionProvider
{
    public IFeatureExtraction build(IPreprocessing poPreprocessing);
}

private final static Map<Integer, IFeatureExtractionProvider> map;

static
{
    map = new HashMap<Integer, IFeatureExtractionProvider>();
    map.put(MARF.LPC, new IFeatureExtractionProvider() {

        @Override
        public IFeatureExtraction build(IPreprocessing poPreprocessing) {
            return new LPC(poPreprocessing);
        }
    });

    // and so on...
}

public static final IFeatureExtraction create(final int piFeatureExtractionMethod, IPreprocessing poPreprocessing)
        throws FeatureExtractionException
{
    IFeatureExtractionProvider provider = map.get(piFeatureExtractionMethod);

    if (provider == null)
        throw new RuntimeException("no provider for " + piFeatureExtractionMethod);

    return provider.build(poPreprocessing);
}

Although you didn't mention Spring or IoC, you did ask for additional ideas... Rather than build the Map of IFeatureExtractionProvider in a static initializer you could combine it with an IoC framework like Spring. Add a new method to the interface (public int getType()). Have your framework inject all instances of IFeatureExtractionProvider to a setter and then add it to the map based on the value of getType().

John R
  • 2,066
  • 1
  • 11
  • 17