简体   繁体   English

使用Java 8中的功能接口,根据类型将数据保存到不同的表中

[英]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 . 我只是在尝试Java 8。 I have a class that has a field called questionType. 我有一个类,有一个名为questionType的字段。 And few other fields in this class should be saved to its respective table according to questionType field. 此类中的其他几个字段应该根据questionType字段保存到其各自的表中。 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. 例如,如果问题类型为MCQ,则其选项应保存在选项表中;如果问题类型为PASSAGE,则其段落应保存在通道表中。

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. 我只是在Java 8上获得概念,说实话,我什至不知道是否有可能在Java 8风格中做。 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. Java 8 Lambda并没有像您的函数那样使用接口,而是快速实现它们。 One would probably implement your function which just uses an interface the same in Java 8 as in previous versions: 一个人可能会实现您的功能,该功能仅使用Java 8中与以前版本相同的接口:

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. 如果人们希望将SaveQuestion接口的许多不同实现传递给saveQuestion函数,则由于SaveQuestion是一个功能接口,因此他们的代码可以更紧凑。 The code inside saveQuestion could be the same regardless. 无论如何,saveQuestion中的代码都可以相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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