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 moreTag Archives: java
Enforce 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 moreStrategy pattern
The name of the pattern always sounded misleading to me – there is no mystical sophisticated strategy that you need to apply to use this pattern. This is all about substituting plain object oriented inheritance with neatly encapsulated algorithms. So, instead of inheriting a behaviour from parent class, we provide a behaviour on instance creation. […]
Read moreJava invokedynamic
Invokedynamic is a JVM bytecode instruction for invoking methods, which facilitates runtime type checking. Before invokedynamic was introduced in Java 1.7, we had 4 opcodes for calling methods: invokevirtual – used for public and protected instance methods invokestatic – used for static methods invokeinterface – used for methods call through an interface invokespecial – used […]
Read more