vendredi 16 mars 2018

Java, how could we abstract three Stateless beans to use an interface and being able to implement strategy design pattern?

I have experience with Java, and I am learning dessign patterns.

I would like to apply the strategy pattern.

Currently the use case is to create three ways to make an assessment:

One is points, 1-10, if you have 5 or more you have passed.

The second are letters, from A-F, A-D mean you have passed.

And the last one are a mix with a label of "Pass" "Not Pass" put by the teacher and a comment. If he gives you a comment and a label of Pass you have passed.

The difficulty I am facing is that one of the three stateless beans has a method with two parameters and the others two have methods with only one parameters; so then I doubt how could we manage to abstract them in an interface.

The idea is to do something like:

enter image description here

The image is taken from Martin Fowler's: Patterns of Enterprise Application Architecture, and there we see the DOmain Model architecture pattern using the Strategy design pattern.

In this use case, the class Products would be the class Courses, and the Recognition Strategy would be Converter Strategy, and each one of its implementations a Stateless Beans.

Here I will show the code of each of the three implementations and how I have tried to approach the interface:

PointsConverter (COnviertePuntosNota.java):

package beans;

import javax.ejb.Stateless;


@Stateless
public class ConviertePuntosNota {



    public String convertidor(int evaluacion){
        return evaluacion >= 5 ? "Apto" : "No apto";
    }
}

LettersCOnverter (LetraNota.java):

package beans;

import javax.ejb.Stateless;

@Stateless
public class LetraNota {

    public String convierteLetraNota(String letra){
        return letra.equals("F") ? "No apto" : letra.equals("E") ? "No apto" : "Apto";
    }
}

LabelAndCommentConverter(ComentarioNota.java):

package beans;

import javax.ejb.Stateless;

@Stateless
public class ComentarioNota {

    public String convierteComentarioNota(String evaluacion, String comentario) {
        if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
            return "Apto";
        } else {
            return "No Apto";
        }
    }
}

I have thought that to generalize different types of inputs we could use generics:

package beans;

public interface ConversorStrategy {
    public String convierteNota(<T> evaluacion, <T> comentario);
}

Add the IDE tell us: Illegal start of expression.

Obviously I am beginner to use generics by my own, and I have used interfaces and abstract classes a few.

How could we address different input's types and input's numbers to use an interface?

I have also read:

https://www.tutorialspoint.com/java/java_generics.htm

Aucun commentaire:

Enregistrer un commentaire