0

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?

  • 4
    Well it depends on your usecase, and how you `treat` `BrandPDFServiceImpl` and `CookbookPDFServiceImpl` since both can have their own implementations ! Let us try to make question more precise? – Harsh Jul 06 '22 at 14:47
  • @Harsh Actually I am looking a proper solution and if you could give examples with proper 2 alternatives, I can stick to one of them. I would be appreciated if you post some examples using the code in my examples (or another code if it is easier for you). Thanks for your helps. –  Jul 06 '22 at 20:34
  • @Jonathan you didn't provide enough information about your services to make a judgement about the current code and it's future state, at current moment I do see about 5-10 options how to organise that but it does not worth to describe all of them without proper inputs. – Andrey B. Panfilov Jul 07 '22 at 05:43
  • @AndreyB.Panfilov Sorry, amigo. Now it's ok and I added full code in the service methods to understand what is going on and what can be done. Any idea? –  Jul 07 '22 at 06:04
  • yep, now it gives more clues about your system. – Andrey B. Panfilov Jul 07 '22 at 06:09
  • @AndreyB.Panfilov Actually at first I thought using Template Method Pattern, but as the common steps is really not much, then maybe it is good idea to use 2 service as in the question by implementing `generatePdf` method. Is that the most proper way? Or what would you suggest? –  Jul 07 '22 at 06:14
  • you do not need `Template Method` there, actually it is good idea to introduce `ContentCreator { Context createContext(DTO dto); }` there, because the only responsibility of your services is to convert DTO to Context – Andrey B. Panfilov Jul 07 '22 at 07:11
  • But there are 2 parameters with different names; "brandName" and "cookbookLogoUrl". Then how could I define `createContext()` method? COuld you post the example code as answer please? –  Jul 07 '22 at 08:33
  • The only responsibility of `CookbookPDFServiceImpl`/`BrandPDFServiceImpl` is to create `Context` and pass it to `TemplateEngine` that is actually means you do not need to implement `PDFService` for each type of DTO - you need to implement converter from `DTO` to `Context` (moreover that will allow you to test each converter individually). Converting `UUID` to `DTO` seems not to be a responsibility of `PDFService` - let caller side to do that. – Andrey B. Panfilov Jul 07 '22 at 09:15
  • Thanks, but what about adding an example code as answer? –  Jul 07 '22 at 09:38

0 Answers0