Design Patterns in Java

How to build better Java code with design patterns

Federico Haag
4 min readDec 30, 2017

Design Patterns are best practices used to solve the most common problems that generally occur during Object Oriented Programming (OOP).

Table of Contents

  1. Creational Patterns
  2. Structural Patterns
  3. Behavioral Patterns
  4. Other Patterns

1. Creational Patterns

Creational patterns deal with object creation mechanisms, providing some special features or abstracting some common tasks.

1.1. Singleton Pattern

Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. Typical Use Cases are logging classes, database connection classes, file manager classes.

1.2. Factory Pattern

The Factory Pattern should be used when you have to create an instance of a class, but depending on some variable input you may have to create a sub-type A or a sub-type B of such a class. In this case, it’s considered good design setting up a class (called Factory) which encapsulate all the logic flows needed to chose which sub-type has to be generate.

Have a look at the following example:

2. Structural Patterns

Structural Patterns deal with the creation of complex object structures through the composition of classes and objects.

2.1. Adapter Pattern

The Adapter Pattern is used in case of need to convert a class interface into another one, for example due to communication issues.

2.2. Decorator Pattern

The Decorator Pattern adds behavior to an object dynamically without affecting the behavior of other objects from the same class.

It is a dynamic alternative to subclassing for extending functionality, particularly when you have to extend functionality at run-time.

2.3. Facade Pattern

The Facade Pattern provides a simplified interface to a larger body of code.

It typically involves a single wrapper class that contains a set of variables and methods required by the client that access the full body of code and hide the implementation details in order to reduce the client perceived complexity of the code.

Credits: https://en.wikipedia.org/wiki/File:Example_of_Facade_design_pattern_in_UML.png

2.4. Proxy Pattern

The Proxy Pattern is used to postpone or avoid instantiation of an heavy object using a lighter object that has the same interface.

The Proxy Pattern is often used in combination with preprocessing techniques, caching, requests queuing.

Sequence Diagram
UML Diagram

3. Behavioral Patterns

Behavioral patterns deal with the communication between objects.

3.1. Strategy Pattern

The Strategy Pattern is used to select at runtime which algorithm has to be executed. It’s useful when a class has different behaviors depending on a variable input. A bad design to solve this use case would be building a huge switch where you insert the all possible algorithms based on the variable input. This solution is considered the best choice as a matter of good OOP design.

UML Diagram

3.2. Iterator Pattern

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Iterators decouple the iteration algorithms from the original data structure making the code lighter and more modular, enabling also multiple concurrents iterations (one iteration <->one iterator).

UML Diagram

3.3. Observer Pattern

The Observer Pattern defines a one-to-many dependance between objects (subject — listeners) in order to notify all the dependents when a change of state occurs in the subject.

Warning: this pattern cause a problem known as “Lapsed listener problem”. The solution is using weak references between subject and listeners.

UML Diagram

3.4. State Pattern

Applying the State Pattern you can create an object with a state variable on which the object behaviors depend.

More eaisly: it’s the object-oriented implementation of a “state machine”.

Unlike the strategy pattern, where the choice on the algorithm to be applied is took base on an input and smoothly applied, the state pattern changes the object behaviors by it self depending on the current object state and as a consequence of a method invocation or internal timing.

UML Diagram

4. Other Patterns

The current best reference you can refer to is Head First Design Patterns.

--

--