Polymorphic Pattern

A fundamental pattern


1 min read

Polymorphic Pattern is presumed as a base-pattern for most of the other popular patterns. It can be used when we have documents that have more similarities than differences. If all the documents in a collection are of similar, but not exactly same and identical structure, we call this the Polymorphic Pattern. It is useful in querying info from a single collection.

Enough of theory and definition stuff let’s dive into practical example. A simple online book store may have different types of books sold.

  • Paperback book
  • E-Book (.pdf or .epub)
  • Audio book

The possible solution for this scenario could be

  • Using a single book collection to store all the three types of books.
  • Using 3 different collections for each book type.

Single books collection structure

Karthik KT Status (2).jpg

From the above picture we can see that most of the different types of books have similar attributes. So in case retrieving all types of books this structure is performant and efficient. If the documents are residing in respective collections a potential time consuming complex join has to be performed to retrieve all the documents at once.

Other potential use cases include:

  • Single View applications ( Insurance Companies, E- Retails, etc.)
  • Content Management
  • Product Catalogs.