1. Overview
The purpose of this article is to explain the basic concept of design patterns and to illustrate some of the most popular patterns.
Using a design pattern you can find a solution to recurring problems, a conceptual tool that helps you to find solutions for a group of problems. In the software design context, they can facilitate the modeling phase and simplify the solving of problems.
There are three type of patterns: Behavioral, Creational, Architectural. This article is not intended to provide in-depth analysis of all the patterns, but to provide an overview and some examples.
2. Singleton
Singleton is a creational design pattern that guarantees that there will only be one instance of a class at any given time. As an example, we can declare a class with a private constructor (that cannot be invoked by client objects), our own static object as private, and synchronized methods to retrieve or delete the current instance of the class.
public class Singleton {
private static MyObject instance = null;
private void Singleton() {}
public static synchronized MyObject getInstance() {
if(instance != null)
return instance;
else {
instance = new MyObject();
return instance;
}
}
}
Singleton is one of the most frequently used design patterns, for example we can see it in the Spring Boot framework. Spring Boot Beans are singletons by default: only one bean instance is created per application, and all bean requests with the same name are provided with a reference to the same bean, for more here
3. MVC - Model View Control
Model View Control is an architectural pattern often used in the development of software systems because it allows us to separate the business logic from the presentation logic for the data and the models that represent the data.
The Model contains all of the data that our application will use, the Controller contains the business logic used to elaborate on the data and the Views provides a representation of the model before and after the controller handles it.
Diffrerence between Spring MVC and Spring Boot:
- Spring MVC: It is a MVC framework used to create web applications, it is http oriented.
- Spring Boot: It is the union of Spring MVC with Auto Configuration (you don't need to write spring.xml file for configurations) and Embedded Server (embedded server like Tomcat allows you to start your standalone application with the jar file).
4. Observer
The Observer is a behavioral pattern used for change state notifications, where defines a 1:n relation between objects which in there is an object called Observable that generates the changes of state and one or more objects called Observers that are interested to the state of the observable object.
Actors:
- Observable: object subject to change of state that is observed.
- Observers: objects interested to the state of the Observable object.
- Communication mechanism: It can be pull or push.
How it works:
The observable object must be capable of notifying the observers when its state changes in both communication mechanisms.
When using the pull method, the observable sends a light weight notification that notifies observers of the state change, then only if the observers need more information about the state will they call the observable to retrieve the desired information. When the observable contains a lot of data, but the observer needs only a subset, this approach is preferred in order to reduce the amount of unnecessary data sent.
Whenever the observers require all the data produced by the observable, the push method is preferable, because it sends all the available data along with the change state notification. The number of communication and the latency are reduced.
5. Saga
In Saga, distributed transactions (transactions between microservices) are managed, meaning that every operation contained in Saga can be rolled back with a compensating operation.
This pattern ensures that every operation will be successfully completed or the rollback to the initial situation will be performed. How? For example with API REST for the rollback.
6. Conclusion
In this article we have seen a first approach to design patterns, and some examples. For more about design patterns read here