diff --git a/layers/README.md b/layers/README.md index c57307eed..aab6e656c 100644 --- a/layers/README.md +++ b/layers/README.md @@ -1,33 +1,40 @@ --- -title: Layers +title: Layered Architecture category: Architectural language: en tag: -- Decoupling + - Abstraction + - Decoupling + - Enterprise patterns + - Layered architecture + - Scalability --- +## Also known as + +* N-Tier Architecture + ## Intent -Layers is an architectural pattern where software responsibilities are divided among the different -layers of the application. +The Layered Architecture pattern helps organize applications into groups of subtasks at different levels of abstraction, facilitating independent development and maintenance of each layer. ## Explanation Real world example -> Consider a website 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. +> 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. +> +> 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. +> +> 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). + In plain words -> 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. +> 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. + Wikipedia says -> 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. +> 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. **Programmatic Example** @@ -59,7 +66,7 @@ public interface CakeBakingService { } ``` -On the top we have our `View` responsible of rendering the cakes. +On the top we have our `View` responsible for rendering the cakes. ```java public interface View { @@ -79,16 +86,44 @@ public class CakeViewImpl implements View { ## Class diagram -![alt text](./etc/layers.png "Layers") +![Layered Architecture](./etc/layers.png "Layered Architecture") ## Applicability +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. + Use the Layers architecture when * 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. +## Known Uses + +* Web applications where the presentation, business logic, and data access layers are distinctly separated. +* Enterprise systems where core functionalities are isolated from interface applications and databases. + +## Consequences + +Benefits + +* Improved manageability with separation of concerns +* Easier to update or modify one layer without affecting others +* Promotes reuse of functionalities. + +Trade-offs + +* Potential performance overhead due to layer interaction +* Complexity in layer management +* Challenges in designing an effective layer distribution. + +## Related Patterns + +* [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. +* Service-Oriented Architecture (SOA): Both patterns emphasize modularization but SOA focuses more on distributed services that can be reused across different systems. + ## Credits -* [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) +* [Pattern-Oriented Software Architecture Volume 1: A System of Patterns](https://amzn.to/3xZ1ELU) +* [Clean Architecture: A Craftsman's Guide to Software Structure and Design](https://amzn.to/3UoKkaR) +* [Java Design Pattern Essentials](https://amzn.to/4drLhHU) diff --git a/layers/pom.xml b/layers/pom.xml index 54ba28667..75d8a135f 100644 --- a/layers/pom.xml +++ b/layers/pom.xml @@ -34,7 +34,6 @@ java-design-patterns 1.26.0-SNAPSHOT - com.iluwatar layers layers layers diff --git a/layers/src/main/java/entity/Cake.java b/layers/src/main/java/entity/Cake.java index 4d673b99e..92dcc5ecf 100644 --- a/layers/src/main/java/entity/Cake.java +++ b/layers/src/main/java/entity/Cake.java @@ -34,11 +34,15 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; import java.util.HashSet; import java.util.Set; +import lombok.Getter; +import lombok.Setter; /** * Cake entity. */ @Entity +@Getter +@Setter public class Cake { @Id @@ -55,30 +59,6 @@ public class Cake { setLayers(new HashSet<>()); } - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public CakeTopping getTopping() { - return topping; - } - - public void setTopping(CakeTopping topping) { - this.topping = topping; - } - - public Set getLayers() { - return layers; - } - - public void setLayers(Set layers) { - this.layers = layers; - } - public void addLayer(CakeLayer layer) { this.layers.add(layer); } diff --git a/layers/src/main/java/exception/CakeBakingException.java b/layers/src/main/java/exception/CakeBakingException.java index 6b38356a9..0af1e28cd 100644 --- a/layers/src/main/java/exception/CakeBakingException.java +++ b/layers/src/main/java/exception/CakeBakingException.java @@ -25,6 +25,7 @@ package exception; +import java.io.Serial; import org.springframework.stereotype.Component; /** @@ -33,6 +34,7 @@ import org.springframework.stereotype.Component; @Component public class CakeBakingException extends Exception { + @Serial private static final long serialVersionUID = 1L; public CakeBakingException() { diff --git a/layers/src/test/java/com/iluwatar/layers/exception/CakeBakingExceptionTest.java b/layers/src/test/java/com/iluwatar/layers/exception/CakeBakingExceptionTest.java index 072603da7..5939abe74 100644 --- a/layers/src/test/java/com/iluwatar/layers/exception/CakeBakingExceptionTest.java +++ b/layers/src/test/java/com/iluwatar/layers/exception/CakeBakingExceptionTest.java @@ -55,8 +55,6 @@ class CakeBakingExceptionTest { * Tests the constructor of {@link CakeBakingException} that accepts a message. * Ensures that an exception created with this constructor correctly stores the provided message * and has {@code null} as its cause. - * - * @param expectedMessage The message provided to the constructor. */ @Test void testConstructorWithMessage() { diff --git a/layers/src/test/java/com/iluwatar/layers/service/CakeBakingServiceImplTest.java b/layers/src/test/java/com/iluwatar/layers/service/CakeBakingServiceImplTest.java index 359e8c16f..cd0edb48c 100644 --- a/layers/src/test/java/com/iluwatar/layers/service/CakeBakingServiceImplTest.java +++ b/layers/src/test/java/com/iluwatar/layers/service/CakeBakingServiceImplTest.java @@ -47,7 +47,6 @@ import service.CakeBakingServiceImpl; /** * Constructs a new instance of CakeBakingServiceImplTest. * - * @param cakeBakingService the service for cake baking operations */ @SpringBootTest(classes = LayersApp.class) class CakeBakingServiceImplTest { diff --git a/layers/src/test/java/com/iluwatar/layers/view/CakeViewImplTest.java b/layers/src/test/java/com/iluwatar/layers/view/CakeViewImplTest.java index 31a1dacf7..1d7546a59 100644 --- a/layers/src/test/java/com/iluwatar/layers/view/CakeViewImplTest.java +++ b/layers/src/test/java/com/iluwatar/layers/view/CakeViewImplTest.java @@ -87,7 +87,7 @@ class CakeViewImplTest { } - private class InMemoryAppender extends AppenderBase { + private static class InMemoryAppender extends AppenderBase { private final List log = new LinkedList<>();