Problem statement - I have to construct an invoice (having line-items, legal details, payment details etc) using booking and payment information of a hotel booking. There are two sources for these two data points (payment+booking) - a third party tool that gives you both and an in-house application that gives you the same but in a different structure. The reason being - lifecycle of some of the bookings are managed by the external tool and the rest by the in-house application.

The resulting invoice has a fixed structure and schema. I am thinking of using strategy pattern for the same as it looks like an obvious choice at the first instance. Any other specific design patterns that would suit this use case?

EDIT - Other design patterns that come to my mind - Factory, Adapter

New contributor
Utsav T is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Do you ever have to combine data from both sources, or do they always come either from source A or from source B? – Kilian Foth Oct 11 at 7:31
  • Given that I have to process 100 bookings in a day, 50 would come from source A and 50 from source B (and subsequently having a different structure). The resulting invoice for all the 100 invoices have to have the same structure. – Utsav T Oct 11 at 7:32
  • 1
    Possible duplicate of Choosing the right Design Pattern – gnat Oct 11 at 7:37
  • @gnat Thanks for pointing out. I went through that answer. I think this question is a little too specific and deserves a non-duplicated existence as a particular use case needs to be discussed and tackled :) – Utsav T Oct 11 at 8:42

Strategy pattern would be best when the behavior of a class should be changed easily.

There are two sources for these two data points (payment+booking) - a third party tool that gives you both and an in-house application that gives you the same but in a different structure.

If we're working with this assumption, then the pattern you're probably looking for is an Adapter. Create a single interface for recovering all the information you require, and in the implementation representing each source of data, you return the requested info.

The adapter then can be used seemlessly in your program for generating the invoice or for anything else you require. If the creation of these adapters is different one from the other, then you may also require the use of a factory method or even an abstract factory.

Let me know if this answers your question! If not write in the comments and I'll correct my answer.

  • Thanks Neil ! I don't think the data from two sources are drastically different - so I think factory would be needed to build these 'not-so-elaborate' adapters. – Utsav T Oct 11 at 8:09
  • Also @Neil, what do you exactly mean when you say "behaviour of a class" ? – Utsav T Oct 11 at 8:34
  • @UtsavT I mean, suppose you want to have a Logger class. You want it to be able to write to system out or to a file at a moments notice. So you use a Strategy pattern to change its behavior without having to switch out implementations of Logger. This is changing behavior. Not very applicable in your case I'd think. – Neil Oct 11 at 9:50

Your Answer

Utsav T is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.