简体   繁体   中英

Using functional interface in java 8 to save data to different table according to its type

I am just trying my hand on java 8 . I have a class that has a field called questionType. And few other fields in this class should be saved to its respective table according to questionType field. for eg if the question type is MCQ it's options should be saved in option table similarly if the question type is PASSAGE it's passage should be saved in passage table.

I have created a functional interface as follows :

@FunctionalInterface
public interface SaveQuestion {
void identifyAndSaveQuestion(Questions q,String type);

}

and my class is :

public class QuestionDTO {
private Questions question;
private List<QuestionAnswer> answers;
private List<QuestionOptions> options;
String QuestionType type;
String passage;

//getter setter
}

and i am not sure but my function should be something like

 void saveQuestion(Questions q,String type,SaveQuestion sq){
    //cant figure out what to do here in java 8 style

     }

I really sorry if you find my question silly. I am just getting concept on java 8 and to be honest i even don't know if it possible to do in java 8 style. So please consider if its a silly question :). Any help will b great help

Java 8 lambdas aren't so much about using an interface as your function does as in quickly implementing them. One would probably implement your function which just uses an interface the same in Java 8 as in previous versions:

void saveQuestion(Questions q,String type,SaveQuestion sq){
    sq.identifyAndSaveQuestion(q,type);
 }

However one could use this to implement new implementations on the fly with less verbose syntax than using anonymous classes. Instead of this:

    saveQuestion(q, type, new SaveQuestion() {

        @Override
        public void identifyAndSaveQuestion(Questions q, String type) {
            System.out.println("type ="+type+", questions="+q);
        }
    });

One could write:

    saveQuestion(q, type, (qs,t) -> {
        System.out.println("type =" + t + ", questions=" + qs);
    });

For a database update you would probably have setup a connection and prepared statement somewhere earlier:

final java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?"
                + "user=somebody&password=mypwd");
final java.sql.PreparedStatement p = con
                .prepareStatement("update mytable set questions=? where type=?");

...

and then later use it:

saveQuestion(q, type, (qs, t) -> {
    try {
         p.setString(1, qs.toString());
         p.setString(2, t);
         p.execute();
    } catch (SQLException ex) {
       // handle exception
    }
});

Notice checked exceptions still have to be caught.

If people want to pass lots of different implementations of your SaveQuestion interface to saveQuestion function, their code can be more compact because SaveQuestion is a functional interface. The code inside saveQuestion could be the same regardless.

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