user19254373
user19254373

Reputation:

Duplicating current service or using Template Method (Design Pattern) in Java?

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?

Upvotes: 0

Views: 154

Answers (0)

Related Questions