Design Pattern for Building an Object from Two Data Sources

billingdesigndesign-patterns

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

Best Answer

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.

Related Topic