Java design patterns: definition and all types

What is a design pattern?

A design pattern is generally a repeatable solution to a commonly occurring software design problem. A design pattern is not a finished design that can be taken and copied into your code. A pattern is not a specific piece of code, but a general concept or template for solving a specific problem that can be used in many different situations. Design patterns can speed up the development process by applying tested and proven development practices.

Distribution of design patterns

We know: creation patterns, structural patterns and behavioural patterns.

Creational patterns

Patterns in this category deal with creating instances from classes.

  • Singleton – ensures that the class has only one instance and provides that as an access point.
  • Builder – allows you to break down the creation of a complex object into a series of simple steps.
  • Prototype – creates an instance of a new object by cloning an existing instance.
  • Factory – provides an interface for creating objects of derived classes.
  • Abstract factory – provides an interface for creating families of related objects without specifying their specific classes.

Structural patterns

Patterns from this category deal with class structure such as inheritance and composition.

  • Adapter – allows objects with incompatible interfaces to work together.
  • Bridge – separates the abstraction from the implementation so that both parts can change independently of each other.
  • Composite – allows you to incorporate objects into tree structures and work with them as individual objects.
  • Decorator – it is used for dynamically adding (and removing) new responsibilities or functionalities to existing objects without affecting the behavior of other objects from the same class.
  • Facade – provides a simplified interface to an otherwise complex system.
  • Flyweight – allows sharing parts of existing objects to reduce the number of created objects.
  • Proxy – provides the ability to replace the original object with another object that controls access to the original object.

Behavioral patterns

Patterns in this category deal with communication between objects.

  • Chain of responsibility – offers a way of passing a request between a chain of objects, where the currently addressed object has the possibility to process it or send it to the next one in the chain.
  • Command – implements the requestresponse model by wrapping a command and its metadata in an object.
  • Iterator – allows you to navigate through the elements of a collection without revealing its base type.
  • Mediator – provides centralized communication between different objects in the system.
  • Memento – allows you to save and subsequently restore the previous state of the object.
  • Observer – provides a mechanism for notifying all interested objects if a change occurs on the observed object.
  • State – allows an object to change its behavior if its internal state changes.
  • Strategy – provides the ability to define a family of algorithms, each in a separate class, and ensure their interchangeability.

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you