Design Principle #2: Consider using the builder pattern when there are many constructor arguments

Before jumping on to builder pattern, let's talk about telescoping constructor (anti)-pattern and JavaBeans pattern. In Java, it is not possible to set default values for constructor parameters. This is where the telescoping constructor pattern comes handy. A class has multiple constructors, where each constructor calls a more specific constructor in the hierarchy, which has … Continue reading Design Principle #2: Consider using the builder pattern when there are many constructor arguments

Design Principle #1: Consider static factory method pattern instead of constructors

In some scenarios, instead of having a constructor, it is better to have a public static factory method that returns an instance of that class. For example, the below method returns an instance of the Boolean object when given boolean primitive type. public static Boolean valueOf(boolean val) { return b?Boolean.TRUE:Boolean.FALSE; } This method has the … Continue reading Design Principle #1: Consider static factory method pattern instead of constructors

Java REST APIs with Protocol Buffers

1. Overview This tutorial will guide you through setting up REST API in Java making the use of binary message streams viz. Google Protocol Buffers (or protobufs). If you are new to Protocol Buffers, I would suggest you to go through first blog of this series, to understand about Google Protocol Buffers and how are … Continue reading Java REST APIs with Protocol Buffers

Java Object Mapper with MapStruct

1. Overview In this article, we will explore how we can use MapStruct framework for mapping one Java object to another. Mapstruct is an object mapping framework just like Dozer. But using Mapstruct over Dozer and other dynamic mapping frameworks gives us below advantages: It is fast in execution as it uses plain method invocations instead … Continue reading Java Object Mapper with MapStruct