简体   繁体   English

将请求参数设置为对象的最佳方法

[英]Best way to set request parameters to object

I need your recommendations. 我需要你的建议。 which way is the best in terms of software engineering (Readability, Usability ) 就软件工程(可读性,可用性)而言,哪种方法是最好的

I have object person 我有对象

public class Person {

    private String name;
    private String surname;

    public Person(String name, String surname) {
        super();
        this.name = name;
        this.surname = surname;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }


}

I have method savePerson . 我有方法savePerson

First way is to set request parameters to variable before initialise object. 第一种方法是在初始化对象之前将请求参数设置为变量。

public void handle(Map<String, Object> map, HttpServletRequest request) throws Exception {
    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    Person person = new Person(name, surname);
    personService.savePerson(person);
}

Second way is to set request parameters set them as constructors parameters. 第二种方法是设置请求参数,将它们设置为构造函数参数。

public void handle(Map<String, Object> map, HttpServletRequest request) throws Exception {
    Person person = new Person(request.getParameter("name"), request.getParameter("surname"));
    personService.savePerson(person);
}

The first way is more readable and easy to debug, than the second one. 第一种方法比第二种方法更具可读性且易于调试。 Moreover, if you add other fields in your Person object the second way will be totally messy. 此外,如果您在Person对象中添加其他字段,则第二种方法将完全混乱。

In order to simply even more the code you can use Lombok to avoid writting Getter and Setter for your Person class 为了简化代码,您可以使用Lombok避免为Person类编写Getter和Setter。

You have to consider that the compiler transforms your operations in a sort of binary version that is a little optimized. 您必须考虑到编译器将您的操作转换为某种经过优化的二进制版本。 Your second version is less readable but it has only one code line. 您的第二个版本可读性较差,但只有一个代码行。 The compiler will do it for you, so you can choose your version according to your writing style. 编译器将为您完成此操作,因此您可以根据自己的写作风格选择版本。 If you have to share your code with other persons, the first method will probably be the best because it is more simple, and in a scenario where the execution efficiency is the same, it can become your discriminant. 如果您必须与其他人共享代码,则第一种方法可能是最好的,因为它更简单,并且在执行效率相同的情况下,它可能成为判别方法。

First one for sure. 当然是第一个。 It is readable and the instructions are clearly visible. 可读性强,说明清晰可见。 It being basic can be understood by any junior level developer as well so if someone else works on this code, it will be easy for him/her to make the necessary changes. 任何初级开发人员都可以理解它的基本知识,因此,如果其他人使用此代码,则他/她将很容易进行必要的更改。

Always the code that is readable must be used so as to keep the things structured and more manageable. 始终必须使用可读的代码,以保持事物的结构化和更易于管理。

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

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