Do you want to know what are design patterns in Java? You came to the correct article, because here we will tell you what they are, as well as we will let you know their classification and a little more.
Object diagram to solve generic problems.
Design patterns in Java
Before talking about the design patterns in Java, we must note that these are focused towards object-oriented programming. Second, it is important to remember some associated basics, including the following:
For additional information, we invite you to read our article called: The Object-oriented programming: Definition.
Associated basics
As we have always said, programming is not an isolated event, so it is always important to remember some related concepts. On this occasion, it is enough to refer to the following aspects:
Java programming language
Java is one of the oldest data programming languages; it is also in popular use on most platforms. In this regard, we can mention that many of the computer applications that we have today are based on this important language.
On this last aspect, we can mention above all the applications related to Web services. For example: mobile applications, desktop, servers, among others.
On the other hand, Java belongs to the object-oriented programming paradigm. In this regard, despite its nature, it is considered a reliable, safe and easy-to-use language.
Additionally, it has the advantage of being able to run on any Java virtual machine, regardless of the computer architecture. In other words, Java is a portable programming language.
Design pattern
A design pattern is a solution to a design problem, which occurs frequently in object-oriented programming. Additionally, this diagram includes the description of the objects by classes, as well as the relationships that connect them.
On the other hand, it is important to mention that design patterns in general are the result of the previous experiences of programmers. Additionally, in the design patterns in Java there is no theorization, as happens with other elements of programming, such as; algorithms.
What are design patterns in Java?
As we have already mentioned, a design pattern is an object diagram that fulfills the purpose of solving a programming problem. Now the design patterns in Java They represent the solution to the problem, embodied in a program written in this specific programming language.
What are the design patterns in Java?
In general terms, we can talk about 23 design patterns in Java. In this regard, these were exposed for the first time in 1995, through the book Dessing Patterns.
On the other hand, as we have already said, these patterns are essential for the resolution of possible new problems. Additionally, they are answers to known problems, originating from the previous experiences of programmers specialized in the object-oriented paradigm.
Types of design patterns in Java
In general, each of the 23 fundamental design patterns is categorized into one of the three general types of design patterns in Java that exist. In this way, below we will show the details about each of them.
Construction patterns
The main function of construction patterns is to organize the creation of objects, encapsulating the use of concrete classes and promoting the interfaces in the relationships between them. To this class of patterns belong the following:
abstract factory
It is responsible for creating objects grouped as a family, without taking into account the specific classes through which the objects are formed.
Builder
It offers the opportunity to create complex objects independently of their implementation or, in other words, through different implementations.
Factory Method
The main function of this pattern is to delegate the creation of an object to specific subclasses. In this regard, he achieves this by using an abstract method.
Prototype
It facilitates the creation of new objects from prototypes from the duplication or cloning of existing objects.
Singleton
The function of this pattern is to manage the existence of classes that have only one instance, as well as to provide a method that is capable of returning said instance.
In this regard, in the following video you can see more information.
Structuring patterns
For their part, the structuring patterns are in charge of organizing the hierarchies of the classes and the relationships that intertwine them. Within this classification are the following:
Adapter
It is responsible for transforming the interface of an existing class into another with the characteristics desired by the users of the system, in such a way that both are compatible. In short, the Adapter pattern is responsible for adapting an existing object.
Bridge
The main function of this pattern is to detach the conceptual aspects belonging to a class hierarchy from its implementation. In other words, this pattern is in charge of implementing an object.
Composite
This type of pattern is responsible for generating a tree design frame, coming from a composition of objects with variable depth. In other words, it is responsible for organizing the hierarchical composition of objects.
Decorator
It is used to add new functionalities, mainly dynamic ones, to an object in order to complement it. In this regard, the result of this pattern is the replacement of an existing object.
About this pattern, you can get more information through the following video.
Facade
Its main objective is to facilitate the use of an interface of objects, so that several of them are regrouped and unified into one. As in the previous pattern, the end result is the replacement of a series of objects.
flyweight
In general terms, the Flayweight pattern is responsible for the sharing process of a series of objects, especially those that have extremely fine granularity. Additionally, it is responsible for keeping an independent state of them.
proxy
The main function of a proxy pattern is to build an object that can be substituted for another, which has full control of its access. In this regard, its main objective is to provide the ideal conditions in terms of optimization and protection of the object.
Behavior patterns
Finally, the behavior patterns are those who provide the solutions to organize the data and objects, additionally, they establish the order between the interactions that occur in the latter. Within this group of patterns we can find the following:
Chain of responsibility
As its name implies, this pattern refers to the creation of a chain of objects, so that when one of them cannot respond to an instruction, the next link can give the corresponding response.
Command
The main objective of this pattern is to convert a query to an object, in such a way that the related basic operations are easier to perform. That is, its function is to transform a request into an object.
Interpreter
It refers to the possibility of offering the representation of a language through grammatical objects. This in order that the written expressions of that language can be easily evaluated and interpreted.
If you want more information about it, you can watch the following video.
iterator
Its main function is to allow sequential access to a group of objects, without the need for system users to know the aspects related to their implementation.
Mediator
As its name implies, this pattern is responsible for constructing an object that acts as a mediator between the interactions of the objects without them interacting directly.
Memento
In general terms, it is one of the most important patterns, since it is he who is in charge of restoring and safeguarding the state of objects. In this regard, we must note that a basic condition within this pattern is respect for the encapsulation of the classes.
Observer
The main function of this pattern is to notify the observers of the modifications that have occurred in the subject, so that they can update their status.
State
The objective of this pattern is quite easy to understand, since it is in charge of offering the opportunity for an object to modify its behavior based on its internal state.
Strategy
It facilitates the adaptation between the behavior and the algorithms of an object, depending on the existence or not of a specific need on the part of the system. In this regard, the interactions between the object and the users of the system should not be altered during the application of this pattern.
template method
This pattern is in charge of making the corresponding report about the stages that make up the operations of an object, which are described in the subclasses. In other words, it is a matter of delegating to the subclasses some stages that make up the operations of an object.
Visitor
It is responsible for the construction of a required operation related to the elements of a series of objects, without any modification occurring in their classes. In short, this pattern is in charge of adding new operations to a series of objects.
Advantages of layout patterns in Java
In general, we can say that the main advantage of the design patterns in Java is that they allow to build complex and robust applications. Additionally, it offers the opportunity to reuse already known solutions in the design of new solutions.
On the other hand, through the use of design patterns in JavaGenerally, designers and developers can strengthen their knowledge of object-oriented programming. In this regard, among the main foundations of this programming paradigm, we can mention the following: polymorphism, inheritance, encapsulation, among others.
How to recognize which design pattern suits our problem?
To identify which design pattern in Java is the one that best responds to a specific programming problem, the first thing we have to know is its objective. In this way, once we have studied the general description of each design pattern, the next thing is to move on to the adaptation stage.
In this regard, it refers to the possibility that the generic structure of the pattern adapts to our problem. To do this, we rename the classes and the methods of said structure and integrate them into our application in an abstract way, this in order to test whether the pattern is indeed capable of responding to our demands.
Finally, if we are on the right track, the last step is to name these classes and methods according to the object they describe and the operations they perform. In this regard, it is important to note that in some cases it is necessary to make modifications to the object diagram.



