Skip to content

Commit 7460e46

Browse files
committed
docs: update layers
1 parent ef42e16 commit 7460e46

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

layers/README.md

+51-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
---
2-
title: Layers
2+
title: Layered Architecture
33
category: Architectural
44
language: en
55
tag:
6-
- Decoupling
6+
- Abstraction
7+
- Decoupling
8+
- Enterprise patterns
9+
- Layered architecture
10+
- Scalability
711
---
812

13+
## Also known as
14+
15+
* N-Tier Architecture
16+
917
## Intent
1018

11-
Layers is an architectural pattern where software responsibilities are divided among the different
12-
layers of the application.
19+
The Layered Architecture pattern helps organize applications into groups of subtasks at different levels of abstraction, facilitating independent development and maintenance of each layer.
1320

1421
## Explanation
1522

1623
Real world example
1724

18-
> Consider a website displaying decorated cakes for weddings and such. Instead of the web page
19-
> directly reaching into the database, it relies on a service to deliver this information. The
20-
> service then queries the data layer to assimilate the needed information.
25+
> Imagine constructing a modern high-rise building, which is analogous to using the Layered Architecture pattern in software development. Just as a building is divided into layers such as the foundation, structural floors, residential floors, and the rooftop, each with specific functions and built using different materials and techniques, a software application can be similarly structured.
26+
>
27+
> In this analogy, the foundation represents the data layer, responsible for managing database operations. The structural floors are akin to the service layer, which contains business logic and rules. The residential floors parallel the presentation layer, which deals with user interfaces and interactions. Finally, the rooftop could be seen as the API layer, allowing external systems to communicate with the application.
28+
>
29+
> Just as each floor in a building is constructed to support the layers above and below, each software layer supports seamless interaction with its neighboring layers while maintaining a degree of independence. This structure allows for easy maintenance and updates, such as refurbishing the interiors (presentation layer) without affecting the underlying structures (business logic and data layers).
30+
2131
In plain words
2232

23-
> With Layers architectural pattern different concerns reside on separate layers. View layer is
24-
> interested only in rendering, service layer assembles the requested data from various sources, and
25-
> data layer gets the bits from the data storage.
33+
> The Layered Architecture pattern organizes software into hierarchical groups of tasks, each encapsulated in distinct layers that interact with each other, facilitating ease of maintenance, scalability, and clear separation of concerns.
34+
2635
Wikipedia says
2736

28-
> In software engineering, multitier architecture (often referred to as n-tier architecture) or
29-
> multilayered architecture is a client–server architecture in which presentation, application
30-
> processing, and data management functions are physically separated.
37+
> 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.
3138
3239
**Programmatic Example**
3340

@@ -59,7 +66,7 @@ public interface CakeBakingService {
5966
}
6067
```
6168

62-
On the top we have our `View` responsible of rendering the cakes.
69+
On the top we have our `View` responsible for rendering the cakes.
6370

6471
```java
6572
public interface View {
@@ -79,16 +86,44 @@ public class CakeViewImpl implements View {
7986

8087
## Class diagram
8188

82-
![alt text](./etc/layers.png "Layers")
89+
![Layered Architecture](./etc/layers.png "Layered Architecture")
8390

8491
## Applicability
8592

93+
This pattern is suitable for structuring applications that can be divided into groups where each group has a specific role or responsibility. Common in enterprise applications, it simplifies dependencies, enhances maintainability, and supports scaling and technology stack segregation.
94+
8695
Use the Layers architecture when
8796

8897
* You want clearly divide software responsibilities into different parts of the program.
8998
* You want to prevent a change from propagating throughout the application.
9099
* You want to make your application more maintainable and testable.
91100

101+
## Known Uses
102+
103+
* Web applications where the presentation, business logic, and data access layers are distinctly separated.
104+
* Enterprise systems where core functionalities are isolated from interface applications and databases.
105+
106+
## Consequences
107+
108+
Benefits
109+
110+
* Improved manageability with separation of concerns
111+
* Easier to update or modify one layer without affecting others
112+
* Promotes reuse of functionalities.
113+
114+
Trade-offs
115+
116+
* Potential performance overhead due to layer interaction
117+
* Complexity in layer management
118+
* Challenges in designing an effective layer distribution.
119+
120+
## Related Patterns
121+
122+
* [Model-View-Controller](https://java-design-patterns.com/patterns/model-view-controller/): Shares separation of concerns by dividing application into input, processing, and output. Layered Architecture often implements an MVC within its presentation layer.
123+
* Service-Oriented Architecture (SOA): Both patterns emphasize modularization but SOA focuses more on distributed services that can be reused across different systems.
124+
92125
## Credits
93126

94-
* [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)
127+
* [Pattern-Oriented Software Architecture Volume 1: A System of Patterns](https://amzn.to/3xZ1ELU)
128+
* [Clean Architecture: A Craftsman's Guide to Software Structure and Design](https://amzn.to/3UoKkaR)
129+
* [Java Design Pattern Essentials](https://amzn.to/4drLhHU)

layers/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
<artifactId>java-design-patterns</artifactId>
3535
<version>1.26.0-SNAPSHOT</version>
3636
</parent>
37-
<groupId>com.iluwatar</groupId>
3837
<artifactId>layers</artifactId>
3938
<name>layers</name>
4039
<description>layers</description>

layers/src/main/java/entity/Cake.java

+4-24
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
import jakarta.persistence.OneToOne;
3535
import java.util.HashSet;
3636
import java.util.Set;
37+
import lombok.Getter;
38+
import lombok.Setter;
3739

3840
/**
3941
* Cake entity.
4042
*/
4143
@Entity
44+
@Getter
45+
@Setter
4246
public class Cake {
4347

4448
@Id
@@ -55,30 +59,6 @@ public Cake() {
5559
setLayers(new HashSet<>());
5660
}
5761

58-
public Long getId() {
59-
return id;
60-
}
61-
62-
public void setId(Long id) {
63-
this.id = id;
64-
}
65-
66-
public CakeTopping getTopping() {
67-
return topping;
68-
}
69-
70-
public void setTopping(CakeTopping topping) {
71-
this.topping = topping;
72-
}
73-
74-
public Set<CakeLayer> getLayers() {
75-
return layers;
76-
}
77-
78-
public void setLayers(Set<CakeLayer> layers) {
79-
this.layers = layers;
80-
}
81-
8262
public void addLayer(CakeLayer layer) {
8363
this.layers.add(layer);
8464
}

layers/src/main/java/exception/CakeBakingException.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package exception;
2727

28+
import java.io.Serial;
2829
import org.springframework.stereotype.Component;
2930

3031
/**
@@ -33,6 +34,7 @@
3334
@Component
3435
public class CakeBakingException extends Exception {
3536

37+
@Serial
3638
private static final long serialVersionUID = 1L;
3739

3840
public CakeBakingException() {

layers/src/test/java/com/iluwatar/layers/exception/CakeBakingExceptionTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ void testConstructor() {
5555
* Tests the constructor of {@link CakeBakingException} that accepts a message.
5656
* Ensures that an exception created with this constructor correctly stores the provided message
5757
* and has {@code null} as its cause.
58-
*
59-
* @param expectedMessage The message provided to the constructor.
6058
*/
6159
@Test
6260
void testConstructorWithMessage() {

layers/src/test/java/com/iluwatar/layers/service/CakeBakingServiceImplTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
/**
4848
* Constructs a new instance of CakeBakingServiceImplTest.
4949
*
50-
* @param cakeBakingService the service for cake baking operations
5150
*/
5251
@SpringBootTest(classes = LayersApp.class)
5352
class CakeBakingServiceImplTest {

layers/src/test/java/com/iluwatar/layers/view/CakeViewImplTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void testRender() {
8787

8888
}
8989

90-
private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
90+
private static class InMemoryAppender extends AppenderBase<ILoggingEvent> {
9191

9292
private final List<ILoggingEvent> log = new LinkedList<>();
9393

0 commit comments

Comments
 (0)