This is a short article on design patterns in Java. There won't be any pattern implementations, just a list of patterns in Java along with a brief description of each. For those already familiar with the subject, this will be helpful as a review and summary. Conversly, those who are learning about patterns for the first time will benefit from this as an initial overview of the topic before digging deeper.
Design patterns are ready-to-use solutions for frequently occurring programming tasks. It's not a class or library that can be connected to a project. It's something more. Design patterns suitable for each task are implemented in each specific case. You should remember that when applied incorrectly or to an unsuitable task, a design pattern can create lots of problems. However, a properly applied pattern can help you complete tasks easily and simply. Types of patterns:
  • creational
  • structural
  • behavioral
Creational patterns provide initialization mechanisms, allowing you to create objects in convenient ways. Structural patterns define relationships between classes and objects, allowing them to work together. Behavioral patterns are used to simplify interaction between entities. Creational:
  • Singleton — restricts the creation of a class to a single instance, and provides access to that single instance.
  • Factory — used when we have a superclass with multiple subclasses and we need to return a subclass based on input.
  • Abstract factory — uses a super factory to create factories, which we then use to create objects.
  • Builder — used to create complex objects using simple objects. It gradually creates a large object from a small, simple object.
  • Prototype — helps improve performance when creating duplicate objects; instead of creating a new object, it creates and returns a clone of an existing object.
  • Adapter — a converter between two incompatible objects. We can use the adapter pattern to combine two incompatible interfaces.
  • Composite — uses one class to represent a tree structure.
  • Proxy — provides the functionality of another class.
  • Flyweight — reuses objects instead of creating a large number of similar objects.
  • Facade — provides a simple interface for a client, which uses the interface to interact with the system.
  • Bridge — makes specific classes independent from classes implementing an interface.
  • Decorator — adds new functionality to an existing object without tying into its structure.
  • Template method — defines a basic algorithm and allows descendants to override some steps of the algorithm without changing its overall structure.
  • Mediator — provides an intermediary class that handles all communication between different classes.
  • Chain of responsibility — makes it possible to avoid strict dependence between the sender and receiver of a request; moreover, the request can be processed by several objects.
  • Observer — allows one object to monitor and respond to events occurring in other objects.
  • Strategy — allows for strategies (algorithms) to be changed at run time.
  • Command — an interface that declares a method for performing a specific action.
  • State — allows an object to change its behavior depending on its state.
  • Visitor — used to simplify operations on groups of related objects.
  • Interpreter — defines a grammar for a simple language in the problem domain.
  • Iterator — sequentially accesses elements of a collection without knowing its underlying form.
  • Memento — used to store an object's state; this state can be restored later.
As you go through the CodeGym course, you'll encounter a couple of patterns on this list. I recommend the following tasks about patterns: 1522, 1530, 1631... Wise use of design patterns leads to more reliable code maintenance, because, in addition to the fact that design patterns are good solutions to common problems, other developers can recognize them, reducing the time required to work with certain code. Design patterns in Java [Part 2]