英文:
Difference global variable in class difference variable in method
问题
我在想将变量声明为全局变量并在服务中使用,以及在方法中使用局部变量的好处可能是什么。可能有好的和不好的地方。例如:
原始代码:
@Service
public class OfficeCodeImpl implements OfficeCodeService {
@Autowired
private OfficeCodeRepository officeCodeRepository;
private List<OfficeCodeDto> list = new ArrayList<>();
private OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
@Override
public List<OfficeCodeDto> getAll() {
officeCodeRepository.findAll().forEach(i -> {
OfficeCodeDto officeCodeDto = new OfficeCodeDto();
BeanUtils.copyProperties(i, officeCodeDto);
list.add(officeCodeDto);
});
return list;
}
@Override
public void save(OfficeCodeDto officeCodeDto) {
BeanUtils.copyProperties(officeCodeDto, officeCodeEntity);
officeCodeRepository.save(officeCodeEntity);
}
}
你的理解是,当你调用这个类时,你只需要在方法内部声明所有需要的变量。如果将其实现为全局变量,那么无论是否需要这些变量,都会生成新的值。
修改后的代码:
@Service
public class OfficeCodeImpl implements OfficeCodeService {
@Autowired
private OfficeCodeRepository officeCodeRepository;
@Override
public List<OfficeCodeDto> getAll() {
List<OfficeCodeDto> list = new ArrayList<>();
officeCodeRepository.findAll().forEach(i -> {
OfficeCodeDto officeCodeDto = new OfficeCodeDto();
BeanUtils.copyProperties(i, officeCodeDto);
list.add(officeCodeDto);
});
return list;
}
@Override
public void save(OfficeCodeDto officeCodeDto) {
OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
BeanUtils.copyProperties(officeCodeDto, officeCodeEntity);
officeCodeRepository.save(officeCodeEntity);
}
}
这两段代码有何不同?
英文:
I was wondering what might be good side of writing need variable as global in service and difference local varible in the method. What might be good or bad? For example
@Service
public class OfficeCodeImpl implements OfficeCodeService {
@Autowired
private OfficeCodeRepository officeCodeRepository;
private List<OfficeCodeDto> list = new ArrayList<>();
private OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
@Override
public List<OfficeCodeDto> getAll() {
officeCodeRepository.findAll().forEach(i ->{
OfficeCodeDto officeCodeDto = new OfficeCodeDto();
BeanUtils.copyProperties(i,officeCodeDto);
list.add(officeCodeDto);
});
return list;
}
@Override
public void save(OfficeCodeDto officeCodeDto) {
BeanUtils.copyProperties(officeCodeDto,officeCodeEntity);
officeCodeRepository.save(officeCodeEntity);
}
}
For me I understand that when you call this class only you need method will declare all varible that inside of this method. If you implement it as global variable it will generate new value if this all varible you need all not need.
@Service
public class OfficeCodeImpl implements OfficeCodeService {
@Autowired
private OfficeCodeRepository officeCodeRepository;
@Override
public List<OfficeCodeDto> getAll() {
List<OfficeCodeDto> list = new ArrayList<>();
officeCodeRepository.findAll().forEach(i ->{
OfficeCodeDto officeCodeDto = new OfficeCodeDto();
BeanUtils.copyProperties(i,officeCodeDto);
list.add(officeCodeDto);
});
return list;
}
@Override
public void save(OfficeCodeDto officeCodeDto) {
OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
BeanUtils.copyProperties(officeCodeDto,officeCodeEntity);
officeCodeRepository.save(officeCodeEntity);
}
}
Difference this two code ?
答案1
得分: 1
使用第二种方法。
在第一种情况下,如果您两次调用getAll
,同一个元素可能会被添加到列表中两次,因此您可能会得到错误的结果。
英文:
Use the second approach.
In the first case, if you call getAll
twice, the same element might be added into the list twice, so you might get the wrong result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论