Java:使用局部变量还是实例变量?
Java: using local vs instance variables?
我的申请中有 3 个 classes
:
1. Runner / Main(调用服务class)
2。服务 class(执行业务逻辑)
3。存储库 class(由服务调用以进行数据库查询)
我不确定在服务中实现变量的最佳方式 class。以下 2 种方法中哪种方法最好,为什么?
例如我应该有 实例变量 :
public class DogService{
List<Dogs> dogList= new ArrayList<Dog>(); //instance var
public DogService(){}
public List<dogs> getAllDogs(){
dogList=dogRepository.getAll();
return dogList;
}
}
或方法中的局部变量:
public class DogService{
public DogService(){}
public List<dogs> getAllDogs(){
List<Dogs> dogList= new ArrayList<Dog>(); //local var to method
dogList=dogRepository.getAll();
return dogList;
}
}
服务使用示例class:
public class Runner {
List<Dogs> listOfAllDogs = new ArrayList<Dog>();
DogService dogService = new DogService();
public static void main(String[] args) {
listOfAllDogs = dogService.getAllDogs();
}
如果 dogList
不会改变,那么将它作为一个字段将允许您潜在地缓存它。对于可能有小狗或死亡的 dogs
可能不是一个好主意,但如果它是静态列表或其他东西,它会有一些用途。
例如
if (dogList == null) {
dogList= new ArrayList<Dog>();
dogList=dogRepository.getAll();
}
return dogList;
在第一种情况下,您使用您的实例创建一个新的 ArrayList,并在您离开该方法后保留对狗列表的引用。你在浪费内存。
此外,您的 class 中有一个您没有使用的字段,因此它会使您的代码变得杂乱无章,您可以删除这些无用的行。
它也可能是错误的来源。此变量已声明,并具有表明其用途的名称。稍后,另一个开发人员可能会尝试将它用于其他用途,并且取决于该方法是否在之前被调用,它会工作还是崩溃。
在第二种情况下,变量没有用,因为您可以立即 return getter 的结果。但编译器会为您处理好,因此您无需担心。
这完全是个人意见,但您误解了服务层的典型用途,即:
public class DogService{
Repository repository;
public DogService(Repository repo){
this.repository = repo;
}
public List<dogs> getAllDogs(){
return this.repository.getAll();
}
}
service
有责任知道去哪里找狗。它不会参与尝试记住特定的狗或查找它们:它 delegates
对底层存储库负责。
这意味着 方法和实例都不应该记住狗的列表。如果另一个方法,例如 getAllDogNames
需要做一些时髦的事情,它可能需要一个实例变量:
public List<String> getAllDogNames(String prefix){
List<Dog> dogs = this.getAllDogs();
List<String> names = new ArrayList<String>();
for (dog : dogs) {
names.add(prefix + dog.getName()); //Or whatever
}
}
但这应该推迟到适当的包装器。
我的申请中有 3 个 classes
:
1. Runner / Main(调用服务class)
2。服务 class(执行业务逻辑)
3。存储库 class(由服务调用以进行数据库查询)
我不确定在服务中实现变量的最佳方式 class。以下 2 种方法中哪种方法最好,为什么?
例如我应该有 实例变量 :
public class DogService{
List<Dogs> dogList= new ArrayList<Dog>(); //instance var
public DogService(){}
public List<dogs> getAllDogs(){
dogList=dogRepository.getAll();
return dogList;
}
}
或方法中的局部变量:
public class DogService{
public DogService(){}
public List<dogs> getAllDogs(){
List<Dogs> dogList= new ArrayList<Dog>(); //local var to method
dogList=dogRepository.getAll();
return dogList;
}
}
服务使用示例class:
public class Runner {
List<Dogs> listOfAllDogs = new ArrayList<Dog>();
DogService dogService = new DogService();
public static void main(String[] args) {
listOfAllDogs = dogService.getAllDogs();
}
如果 dogList
不会改变,那么将它作为一个字段将允许您潜在地缓存它。对于可能有小狗或死亡的 dogs
可能不是一个好主意,但如果它是静态列表或其他东西,它会有一些用途。
例如
if (dogList == null) {
dogList= new ArrayList<Dog>();
dogList=dogRepository.getAll();
}
return dogList;
在第一种情况下,您使用您的实例创建一个新的 ArrayList,并在您离开该方法后保留对狗列表的引用。你在浪费内存。
此外,您的 class 中有一个您没有使用的字段,因此它会使您的代码变得杂乱无章,您可以删除这些无用的行。
它也可能是错误的来源。此变量已声明,并具有表明其用途的名称。稍后,另一个开发人员可能会尝试将它用于其他用途,并且取决于该方法是否在之前被调用,它会工作还是崩溃。
在第二种情况下,变量没有用,因为您可以立即 return getter 的结果。但编译器会为您处理好,因此您无需担心。
这完全是个人意见,但您误解了服务层的典型用途,即:
public class DogService{
Repository repository;
public DogService(Repository repo){
this.repository = repo;
}
public List<dogs> getAllDogs(){
return this.repository.getAll();
}
}
service
有责任知道去哪里找狗。它不会参与尝试记住特定的狗或查找它们:它 delegates
对底层存储库负责。
这意味着 方法和实例都不应该记住狗的列表。如果另一个方法,例如 getAllDogNames
需要做一些时髦的事情,它可能需要一个实例变量:
public List<String> getAllDogNames(String prefix){
List<Dog> dogs = this.getAllDogs();
List<String> names = new ArrayList<String>();
for (dog : dogs) {
names.add(prefix + dog.getName()); //Or whatever
}
}
但这应该推迟到适当的包装器。