简体   繁体   中英

I want to implement DRY principle but don't know how

I have this method in Java.

    public double[] pruebas(int dimension, String prueba) {
    random rd1 = new random(semilla, numSol); 
    double mejorSolucion[] = new double[dimension];


    switch (prueba) {
        case "schwefelUnimodal":
            for (int i = 0; i < numSol; i++) {
                for (int j = 0; j < dimension; j++) {
                    tresDimenciones[j] = schwefelUnimodal(numSol, rd1.nextInt());
                    if (tresDimenciones[j] < mejorSolucion[j]) {
                        mejorSolucion[j] = tresDimenciones[j];
                    }
                }
                System.out.println("///////////Corrida " + i);
                System.out.println("Primera:" + tresDimenciones[0] + "\nSegunda:" + tresDimenciones[1] + "\ntercera:" + tresDimenciones[2]);
            }
            break;
        case "schwefelMultimodal":
            for (int i = 0; i < numSol; i++) {
                for (int j = 0; j < dimension; j++) {
                    tresDimenciones[j] = schwefelMultimodal(numSol, rd1.nextInt());
                    if (tresDimenciones[j] < mejorSolucion[j]) {
                        mejorSolucion[j] = tresDimenciones[j];
                    }
                }
                System.out.println("///////////Corrida " + i);
                System.out.println("Primera:" + tresDimenciones[0] + "\nSegunda:" + tresDimenciones[1] + "\ntercera:" + tresDimenciones[2]);
            }
            break;
        case "rosenbrock":
            for (int i = 0; i < numSol; i++) {
                for (int j = 0; j < dimension; j++) {
                    tresDimenciones[j] = rosenbrock(numSol, rd1.nextInt(), rd1.nextInt());
                    if (tresDimenciones[j] < mejorSolucion[j]) {
                        mejorSolucion[j] = tresDimenciones[j];
                    }
                }
                System.out.println("///////////Corrida " + i);
                System.out.println("Primera:" + tresDimenciones[0] + "\nSegunda:" + tresDimenciones[1] + "\ntercera:" + tresDimenciones[2]);
            }
            break;

    }

    return mejorSolucion;
}

It calls other methods, it shows here just 3(schwefelUnimodal,schwefelMultimodal and rosenbrock) but i have to use another 6, all of them use the same parameters(both int)and have to do the same(the 2 for loops). I understand that Java does not pass methods as parameters.

I'm thinking on interfaces(I'm not really sure, i don't know much about interfaces) but i need a little guidance on how to do it or i want to know if there is another options.

When you're looking for things to deduplicate/dry up, you should look for commonality between functions.

First and foremost: interfaces would not serve you any gains here. You use an interface to enforce a specific contract between all implementors of said interface; that is to say, irrespective of what implementation you use for a List , they will all have size() .

Now, to your main point - you have very, very common code within your switch statement which appears to duplicate itself except for the different method call.

The real difference is the array you're providing, but you also have a lot of other stuff you bring in as constant state, too. So, you could simply create a method which abstracted out the difference in the tresDimenciones array.

public void doOperation(int dimension, int numSol, double[] tresDimenciones, double[] mejorSolucion) {
    for (int i = 0; i < numSol; i++) {
        for (int j = 0; j < dimension; j++) {
            if (tresDimenciones[j] < mejorSolucion[j]) {
                mejorSolucion[j] = tresDimenciones[j];
            }
        }
        System.out.println("///////////Corrida " + i);
        System.out.println("Primera:" + tresDimenciones[0] + "\nSegunda:" + tresDimenciones[1] + "\ntercera:" + tresDimenciones[2]);
    }
}

Integrating this into your application is an exercise I leave for the reader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM