2015-08-15 21:44:08 +03:00
---
layout: pattern
title: Layers
folder: layers
permalink: /patterns/layers/
2017-03-25 22:07:10 +01:00
pumlformat: svg
2015-08-20 21:40:07 +02:00
categories: Architectural
2021-05-19 10:49:05 -06:00
language: en
2015-12-28 15:52:44 +02:00
tags:
2019-12-13 21:09:28 +02:00
- Decoupling
2015-08-15 21:44:08 +03:00
---
2016-01-03 21:14:30 +01:00
## Intent
2020-08-29 21:38:58 +03:00
Layers is an architectural pattern where software responsibilities are divided among the different
layers of the application.
2015-08-15 21:44:08 +03:00
2019-11-05 20:07:50 +02:00
## Explanation
Real world example
2020-08-29 21:38:58 +03:00
> Consider a web site displaying decorated cakes for weddings and such. Instead of the web page
> directly reaching into the database, it relies on a service to deliver this information. The
> service then queries the data layer to assimilate the needed information.
2019-11-05 20:07:50 +02:00
2020-08-29 21:38:58 +03:00
In plain words
2019-11-05 20:07:50 +02:00
2020-08-29 21:38:58 +03:00
> With Layers architectural pattern different concerns reside on separate layers. View layer is
> interested only in rendering, service layer assembles the requested data from various sources, and
> data layer gets the bits from the data storage.
2019-11-05 20:07:50 +02:00
Wikipedia says
2020-08-29 21:38:58 +03:00
> In software engineering, multitier architecture (often referred to as n-tier architecture) or
> multilayered architecture is a client– server architecture in which presentation, application
> processing, and data management functions are physically separated.
2019-11-05 20:07:50 +02:00
**Programmatic Example**
2020-08-29 21:38:58 +03:00
On the data layer, we keep our cake building blocks. `Cake` consist of layers and topping.
2019-11-05 20:07:50 +02:00
```java
@Entity
public class Cake {
@Id
@GeneratedValue
private Long id;
@OneToOne (cascade = CascadeType.REMOVE)
private CakeTopping topping;
@OneToMany (cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
private Set< CakeLayer > layers;
}
```
2020-08-29 21:38:58 +03:00
The service layer offers `CakeBakingService` for easy access to different aspects of cakes.
2019-11-05 20:07:50 +02:00
```java
public interface CakeBakingService {
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
List< CakeInfo > getAllCakes();
void saveNewTopping(CakeToppingInfo toppingInfo);
List< CakeToppingInfo > getAvailableToppings();
void saveNewLayer(CakeLayerInfo layerInfo);
List< CakeLayerInfo > getAvailableLayers();
}
```
2020-08-29 21:38:58 +03:00
On the top we have our `View` responsible of rendering the cakes.
2019-11-05 20:07:50 +02:00
```java
public interface View {
void render();
}
2021-03-13 13:19:21 +01:00
@Slf4j
2019-11-05 20:07:50 +02:00
public class CakeViewImpl implements View {
2020-07-30 20:28:47 +03:00
private final CakeBakingService cakeBakingService;
2019-11-05 20:07:50 +02:00
public CakeViewImpl(CakeBakingService cakeBakingService) {
this.cakeBakingService = cakeBakingService;
}
public void render() {
cakeBakingService.getAllCakes().forEach(cake -> LOGGER.info(cake.toString()));
}
}
```
2019-12-07 20:01:13 +02:00
## Class diagram
2020-08-29 21:38:58 +03:00
2019-12-07 20:01:13 +02:00

## Applicability
2020-08-29 21:38:58 +03:00
2019-12-07 20:01:13 +02:00
Use the Layers architecture when
2020-08-29 21:38:58 +03:00
* You want clearly divide software responsibilities into different parts of the program.
* You want to prevent a change from propagating throughout the application.
* You want to make your application more maintainable and testable.
2019-12-07 20:01:13 +02:00
2016-01-03 21:14:30 +01:00
## Credits
2015-09-24 12:23:02 +05:30
2020-07-06 13:31:07 +03:00
* [Pattern Oriented Software Architecture Volume 1: A System of Patterns ](https://www.amazon.com/gp/product/0471958697/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0471958697&linkCode=as2&tag=javadesignpat-20&linkId=e3f42d7a2a4cc8c619bbc0136b20dadb )