mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-09-05 00:18:26 +00:00
* #2450 1. Refactored Layers Architecture to be a Spring boot app. 2. Changed the xml-based configuration to annotation-based. 3. Used spring's constructor injection to pass around needed objects. 4. Implemented deleteAll() methods for the CakeBakingServiceImpl to be used during testing 5. Implemented a CommandLineRunner to run the spring boot app. And others. * #2450 added the contents of the etc directory and a README.md * #2450 made corrections in response to the PR tests * #2450 made corrections in response to requested changes * #2450 made corrections in response to requested changes --------- Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
This commit is contained in:
co-authored by
Ilkka Seppälä
parent
e40923d938
commit
edbb59a982
+9
-27
@@ -3,34 +3,31 @@ title: Layers
|
||||
category: Architectural
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Decoupling
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
Layers is an architectural pattern where software responsibilities are divided among the different
|
||||
Layers is an architectural pattern where software responsibilities are divided among the different
|
||||
layers of the application.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> 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
|
||||
> 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.
|
||||
|
||||
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
|
||||
> 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.
|
||||
|
||||
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
|
||||
> 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**
|
||||
|
||||
On the data layer, we keep our cake building blocks. `Cake` consist of layers and topping.
|
||||
@@ -38,14 +35,11 @@ On the data layer, we keep our cake building blocks. `Cake` consist of layers an
|
||||
```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;
|
||||
}
|
||||
@@ -55,17 +49,11 @@ The service layer offers `CakeBakingService` for easy access to different aspect
|
||||
|
||||
```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();
|
||||
}
|
||||
```
|
||||
@@ -74,20 +62,14 @@ On the top we have our `View` responsible of rendering the cakes.
|
||||
|
||||
```java
|
||||
public interface View {
|
||||
|
||||
void render();
|
||||
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public class CakeViewImpl implements View {
|
||||
|
||||
private final CakeBakingService cakeBakingService;
|
||||
|
||||
public CakeViewImpl(CakeBakingService cakeBakingService) {
|
||||
this.cakeBakingService = cakeBakingService;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
cakeBakingService.getAllCakes().forEach(cake -> LOGGER.info(cake.toString()));
|
||||
}
|
||||
@@ -108,4 +90,4 @@ Use the Layers architecture when
|
||||
|
||||
## 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://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)
|
||||
Reference in New Issue
Block a user