Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中包装类而不重复所有方法_Java_Design Patterns_Aop_Spring Aop - Fatal编程技术网

在Java中包装类而不重复所有方法

在Java中包装类而不重复所有方法,java,design-patterns,aop,spring-aop,Java,Design Patterns,Aop,Spring Aop,我想为多租户系统编写一个Java类,以特定的方式包装另一个类: 原始类的所有方法都应该存在于新类中 除了新方法的参数应该是相同的减去1:租户(=当前用户) 我想为新类编写尽可能少的代码。在最好的情况下,它应该是空的,并且在运行时所有方法都“自动生成”或“自动检测” 例如: 以现有课程为例: MyService{ public List<Product> getProducts(Account filterWithUseraccount); public List&l

我想为多租户系统编写一个Java类,以特定的方式包装另一个类:

  • 原始类的所有方法都应该存在于新类中
  • 除了新方法的参数应该是相同的减去1:租户(=当前用户)
  • 我想为新类编写尽可能少的代码。在最好的情况下,它应该是空的,并且在运行时所有方法都“自动生成”或“自动检测”

例如:

以现有课程为例:

MyService{
   public List<Product> getProducts(Account filterWithUseraccount);
   public List<Purchase> getPurchases(Account filterWithUseraccount, Date fromDate, Date tillDate);
   public List<Category> getCategories(Account filterWithUseraccount, Category parentCategory);
}
MyService{
公共列表getProducts(帐户筛选器WithUserAccount);
公共列表getPurchases(帐户过滤器,带用户帐户,日期fromDate,日期tillDate);
公共列表getCategories(帐户筛选器,其中包含UserAccount、Category parentCategory);
}
我想创建这个新类,但方法应该是“自动生成的”:

MyTenantorEntedService{
//分配一个用于调用所有MyService方法的帐户
私人帐户过滤器,带有用户帐户;
//正在打包的服务
私人MyService-MyService;
公共列表产品(){
返回myService.getProducts(filterWithUseraccount);
}
公共列表getPurchases(日期fromDate,日期tillDate){
返回myService.getPurchases(filterWithUseraccount、fromDate、tillDate);
}
公共列表getCategories(类别ParentCategories){
返回myService.getCategories(filterWithUseraccount、parentCategory);
}
}

如您所见,MyTenantorEntedService中的方法与MyService中的方法相同,只是参数“Account filterWithUseraccount”从未使用过,并且可以从私有属性中获取

这是一个简单的示例,但请想象一个包含许多方法的类,这使得创建包装函数成为一项艰巨的任务。这是大量的代码重复,很容易产生bug


我相信可能存在使用AOP或其他设计模式的解决方案,但我不知道是哪个。

您的IDE为您处理此类任务

例如,您可以在MyTenantorEntedService中添加类型为MyService的成员变量,在Eclipse上,右键单击MyTenantorEntedService,然后单击“源->生成委托方法”


它正在为您申请授权。

授权模式正是我所需要的。有AOP替代方案吗?我在Spring中见过几个类,它们的主体是空的,但似乎仍然包含方法。我想知道他们是如何做到的……是的,你可以通过AOP或动态代理来做到“同样”。选中“相同”,因为通过使用AOP或动态代理,您是在运行时而不是在编译时装饰类。在选择其中一个时应该考虑什么?意味着AOP与使用IDE维护代码?我想保持代码与IDE同步在某些阶段也可能会导致问题。维护代码不应该是问题,这取决于MyService何时更改以及更改者(是第三方类吗?)。使用AOP,您将有更大的运行时开销和打包大小
MyTenantOrientedService{

   // Assign an account which will be used to call all MyService methods
   private Account filterWithUseraccount;

   // The service being wrapped
   private MyService myService;

   public List<Product> getProducts(){
      return myService.getProducts(filterWithUseraccount);
   }
   public List<Purchase> getPurchases(Date fromDate, Date tillDate){
      return myService.getPurchases(filterWithUseraccount, fromDate, tillDate);
   }
   public List<Category> getCategories(Category parentCategory){
      return myService.getCategories(filterWithUseraccount, parentCategory);
   }
}