1

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

asked Oct 11, 2018 at 7:24
4
  • 1
    Do you ever have to combine data from both sources, or do they always come either from source A or from source B? Commented Oct 11, 2018 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. Commented Oct 11, 2018 at 7:32
  • 1
    Possible duplicate of Choosing the right Design Pattern Commented Oct 11, 2018 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 :) Commented Oct 11, 2018 at 8:42

1 Answer 1

2

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.

answered Oct 11, 2018 at 7:35
3
  • 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. Commented Oct 11, 2018 at 8:09
  • Also @Neil, what do you exactly mean when you say "behaviour of a class" ? Commented Oct 11, 2018 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. Commented Oct 11, 2018 at 9:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.