This one is as easy as it gets. You simply create a class which exposes a simple API and hides the complexity behind it. Facade pattern code example: public class Facade { public int simpleCall() { ComplicatedObject1 obj1 = process(); ComplicatedObject2 obj2 = processAgain(obj1); return obj2.getTheMeaningOfLife(); } }
Read moreMonthly Archives: March 2018
How to answer architecture and system design interview questions
Not so long ago I have applied for a senior position in one of big IT companies. The recruitment process consisted of several parts: online coding and 4 interviews. One of them was architecture + system design interview. How did I do? Well, the polite way to say it, would be that I did not […]
Read moreEnforce noninstantiability with a private constructor
Sometimes we want to create a class that is non instantiable: a class of static helper methods or constants. Let’s see how to make sure that such a class is never instantiated. Why should’t we have separate helper method classes? In general, it is not considered a good object oriented design. But the world is not […]
Read moreEnforce the singleton property with a private constructor or an enum type
A singleton is an object that is instantiated once and once only during runtime of an application. How can it be achieved? There are three common ways to do it: public static final field instance, static factory, enum. What is the easiest way to implement a singleton? Have a public static final field instance and […]
Read moreDecorator pattern
A decorator extends base object by modifying its behaviour. Each decorator contains a base object type reference, and at the same time is of base object type. That way, decorators can be chained one over another, and none of them know, if the reference base object they contain is in fact a concrete base object […]
Read moreConsider a builder when faced with many constructor parameters
Constructors with many parameters are hard to read. The only clue you have about what each parameter means is its order. It is not easily readable. Even more troubles emerge, when we have optional parameters. Then we start to drown in a sea of constructors with different set of parameters, a so called telescopic constructor […]
Read moreConsider static factory methods instead of constructors
Event though a constructor is a fairly standard and natural way of creating instances of a class, there are other ways to do it. And they might be sometimes better suited to do the job that needs to be done. You ill find this pattern in many libraries API, just think of Integer.valueOf() or getInstance(). All […]
Read more