在Java中复制当前服务或使用模板方法(设计模式)?

发布于 2025-02-13 09:53:19 字数 1688 浏览 0 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文