vendredi 4 novembre 2016

Is there a better OOP approach to nesting strategy patterns?

Is there a better OOP approach to nesting strategy patterns?

I have a Genetic Algorithm that I want to be able to choose a Fitness Algorithm and Fitness Functions independently at runtime. The Fitness Algorithm will use the Fitness Function through Composition. Currently, I am using the Strategy Pattern with a Factory method (SPFM) to choose the algorithm used and then using another SPFM for choosing the Fitness Functions used by the algorithm.

Fitness Algorithm SPFM

public interface I_Fitness {
    //Accessors
    public Population orderByFitness(Population population);
}

public class FitnessFactory {
    public static I_Fitness fitnessAlgorithmFactory(String fitnessType){
        if(fitnessType.equalsIgnoreCase("cumulative")){return new FitnessCumulative();}
        if(fitnessType.equalsIgnoreCase("weighted")){return new FitnessWeighted();}

        return null;
    }
}

public class FitnessCumulative implements I_Fitness {

    @Override
    public Population orderByFitness(Population population) {
        I_FitnessFunctions fitnessFunctions =  fitnessFunctionFactory(fitnessFunctionType);
        ...
    }
}

public class FitnessWeighted implements I_Fitness {

    @Override
    public Population orderByFitness(Population population) {
        I_FitnessFunctions fitnessFunctions =  fitnessFunctionFactory(fitnessFunctionType);
        ...
    }
}

Fitness Function SPFM

public interface I_FitnessFunctions {

    //Accessors
    public Double[]     getFitness(Chromosome chromosome);
}

public class FitnessFunctionsFactory {
    public static I_FitnessFunctions fitnessFunctionsFactory(String fitnessFunctionType){
        if(fitnessFunctionType.equalsIgnoreCase("FitnessFunctions1")){return new fitnessFunctions1();}
        if(fitnessFunctionType.equalsIgnoreCase("FitnessFunctions2")){return new fitnessFunctions2();}

        return null;
    }
}

public class fitnessFunctions1 implements I_FitnessFunctions {

    @Override
    public Double getFitness(Chromosome chromosome) {
        ...
    }
}

public class fitnessFunctions2 implements I_FitnessFunctions {

    @Override
    public Double getFitness(Chromosome chromosome) {
        ...
    }
}

Aucun commentaire:

Enregistrer un commentaire