Java中具有模式的重复代码重构
作者:互联网
Java比Dropwizard更重要.但是我在Dropwizard中有两个资源:CustomerResource和ApiResource.
在CustomerResource中,有一个createCustomer方法,该方法基本上会创建一个新客户.当第三方调用其中的一个方法时,ApiResource还将创建一个新客户,因此这让我考虑了重复代码以及解决该问题的最佳方法.我想到了几种方法;但首先是为了更清楚起见的类.
@Path("/internal")
public class CustomerResource{
private DBDao dbDao;
private AnotherAPI api;
//constructor for DI
public Response Create(@internalAuth CustomerPojo customerPojo) {
//logic to validate customerpojo
//logic to ensure user isn't a duplicate
//some other validation logic
//finally user creation/saving to DB
Return response.ok(200).build();
}
}
@Path("/external")
public class ApiResource{
private DBDao dbDao;
private AnotherAPI api;
//constructor for DI
public Response Create(@ExternalAuth PartialCustomerPojo partialCustomerPojo) {
//logic to validate PartialCustomerpojo
//supplement partialCustomerPojo
//logic to ensure user isn't a duplicate
//some other validation logic
//finally user creation/saving to DB
Return response.ok(200).build();
}
}
因此,两个主要区别是端点的调用方式(身份验证)和所提供的有效负载.
我考虑删除重复代码的方式是创建一个新的具体类,该类从这两种资源中获取通用性,并且每个资源都实例化这样的新类.
public class CommonClass{
private DBDao dbDao;
private AnotherAPI api;
//constructor for DI
public boolean Create (CommonPojo commonPojo) {
//logic to validate customerPojo
//logic to ensure user isn't a duplicate
//some other validation logic
//finally user creation/saving to DB
Return response.ok(200).build();
}
}
现在在CustomerResource和ApiResource内部,我只需执行此操作.
CommonClass commonClass = new CommonClass(dbDao, api);
//create a new instance customerPojo or CommonPojo and call
commonClass.create(customerPojo);
这听起来像是个好策略吗?除了重复之外还有其他问题吗?这两个资源方法也不能在同一个类中.任何最佳做法将不胜感激.
解决方法:
我认为继承不是最好的解决方案.
我也认为构图要好得多.这可以帮助您使用通用代码,并易于在需要更改功能的其他地方进行更改.
它还使您可以更轻松地测试所有类.
例如:
class CommonPojo {}
class CustomerPojo extends CommonPojo {}
class PartialCustomerPojo extends CommonPojo {}
interface IResourceValid {
boolean isResourceValid(CommonPojo pojo);
}
class CustomerPojoValidator implements IResourceValid {
@Override
public boolean isResourceValid(CommonPojo pojo) {
//your validation for customer
return false;
}
}
class PartialCustomerPojoValidator implements IResourceValid {
@Override
public boolean isResourceValid(CommonPojo pojo) {
//your validation for partial customer
return true;
}
}
class CommonResource{
private DBDao dbDao;
private AnotherAPI api;
private IResourceValid validator;
public IResourceValid getValidator() {
return validator;
}
//constructor for DI
public Response Create(CommonPojo commonPojo) {
//logic to validate customerpojo
//logic to ensure user isn't a duplicate
//some other validation logic
//finally user creation/saving to DB
validator.isResourceValid(commonPojo);
return response.ok(200).build();
}
}
//@Path("/internal")
class CustomerResource{
private CommonResource resource;
//constructor for DI
public Response Create(CustomerPojo CustomerPojo) {
return resource.Create(CustomerPojo);
}
}
//@Path("/external")
class ApiResource{
private CommonResource resource;
//constructor for DI
public Response Create(PartialCustomerPojo partialCustomerPojo) {
return resource.Create(partialCustomerPojo);
}
}
DBDao dao = new DBDao();
AnotherAPI api = new AnotherAPI();
CommonResource castomerCreator = new CommonResource(new CustomerPojoValidator(), dao, api);
CommonResource apiCreator = new CommonResource(new PartialCustomerPojoValidator(), dao, api);
CustomerResource customerResource = new CustomerResource(castomerCreator);
ApiResource apiResource = new ApiResource(apiCreator);
customerResource.Create(somePojo);
apiResource.Create(someAnotherPojo);
标签:dropwizard,java,design-patterns,refactoring 来源: https://codeday.me/bug/20191024/1923423.html