Tag Archives: programming patterns

Bridge pattern

Bridge design pattern is the essence of Object Oriented programming best practices. It enforces encapsulation, separation of responsibilities and coding to interfaces. It decouples the classes while maintaining a clear interface between them. Let’s see how it works. Let’s imagine that we have two types of classes, and we would like them to cooperate, in […]

Read more

Adapter pattern

Adapter programming pattern comes in handy, when we would like to use a certain API, but have an object with a different interface. When so, we can use the adapter to wrap the strange interface with convenient API of ours. Plus, we can adapt the output to our needs. What are the scenarios where we […]

Read more

Proxy pattern

The idea behind the proxy design pattern is to have a class, which in some way facilitates or optimises access to an underlying object. At the same time, the proxy class has the same interface as the underlying object, so it can be substituted for it. Proxy pattern can be used for accessing remote resources, […]

Read more

Facade design pattern

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 more

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 more

Decorator 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 more

Consider 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 more

Consider 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

Strategy 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 more