在Java中复制当前服务或使用模板方法(设计模式)?
在我的Java(Spring Boot)应用中,我拥有以下PDF服务,该服务使用brandService
:
服务:
public interface PDFService<T, S> {
String generatePdf(UUID uuid);
}
brandServiceimpl:
@Service
@RequiredArgsConstructor
public class BrandPDFServiceImpl implements PDFService {
private final BrandService brandService;
public String generatePdf(UUID uuid) {
Context context = new Context();
BrandDTO brandDTO = brandService.findByUuid(uuid);
context.setVariable("brandName", brandDTO.getName());
context.setVariable("brandLogoUrl", brandDTO.getImageUrl());
setProductInformationToHtml(context, brandDTO);
return templateEngine.process("cookbookTemplate", context);
}
}
现在我需要重复<代码> brandpdfserviceimpl 作为copbookpdfserviceimpl
,我可以轻松执行此操作,如下所示:
cookbookpdfserviceimpl:
@Service
@RequiredArgsConstructor
public class CookbookPDFServiceImpl implements PDFService {
private final CookbookService cookbookService;
public String generatePdf(UUID uuid) {
Context context = new Context();
CookbookDTO cookbookDTO = cookbookService.findByUuid(uuid);
context.setVariable("cookbookName", cookbookDTO.getName());
context.setVariable("cookbookLogoUrl", cookbookDTO.getImageUrl());
setProductInformationToHtml(context, cookbookDTO);
return templateEngine.process("cookbookTemplate", context);
}
}
但是,我不确定是否应该使用模板方法
为公共generatepdf
方法设计模式。那么,在这个场景中,最合适的方法是什么?
In my Java (Spring Boot) app, I have the following pdf service that uses BrandService
:
Service:
public interface PDFService<T, S> {
String generatePdf(UUID uuid);
}
BrandServiceImpl:
@Service
@RequiredArgsConstructor
public class BrandPDFServiceImpl implements PDFService {
private final BrandService brandService;
public String generatePdf(UUID uuid) {
Context context = new Context();
BrandDTO brandDTO = brandService.findByUuid(uuid);
context.setVariable("brandName", brandDTO.getName());
context.setVariable("brandLogoUrl", brandDTO.getImageUrl());
setProductInformationToHtml(context, brandDTO);
return templateEngine.process("cookbookTemplate", context);
}
}
Now I need to duplicate BrandPDFServiceImpl
as CookbookPDFServiceImpl
, and I can do this easily as shown below:
CookbookPDFServiceImpl:
@Service
@RequiredArgsConstructor
public class CookbookPDFServiceImpl implements PDFService {
private final CookbookService cookbookService;
public String generatePdf(UUID uuid) {
Context context = new Context();
CookbookDTO cookbookDTO = cookbookService.findByUuid(uuid);
context.setVariable("cookbookName", cookbookDTO.getName());
context.setVariable("cookbookLogoUrl", cookbookDTO.getImageUrl());
setProductInformationToHtml(context, cookbookDTO);
return templateEngine.process("cookbookTemplate", context);
}
}
However, I am not sure if I should use Template Method
design pattern for the common generatePdf
method. So, in this scene, what is the most proper way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论