Update README.md

This commit is contained in:
Ilkka Seppälä 2020-08-29 21:38:58 +03:00
parent bcca9beb4d
commit 3544a8366f
2 changed files with 27 additions and 30 deletions

View File

@ -10,26 +10,33 @@ tags:
---
## Intent
Layers is an architectural pattern where software responsibilities are
divided among the different layers of the application.
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 service then queries the data layer to assimilate the needed information.
> 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.
In Plain Words
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.
> 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 clientserver 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 clientserver 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. Cakes consist of layers and topping.
On the data layer, we keep our cake building blocks. `Cake` consist of layers and topping.
```java
@Entity
@ -47,7 +54,7 @@ public class Cake {
}
```
The service layer offers CakeBakingService for easy access to different aspects of cakes.
The service layer offers `CakeBakingService` for easy access to different aspects of cakes.
```java
public interface CakeBakingService {
@ -66,7 +73,7 @@ public interface CakeBakingService {
}
```
On the top we have our view responsible of rendering the cakes.
On the top we have our `View` responsible of rendering the cakes.
```java
public interface View {
@ -92,14 +99,16 @@ public class CakeViewImpl implements View {
```
## Class diagram
![alt text](./etc/layers.png "Layers")
## Applicability
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
* 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.
## Credits

View File

@ -35,9 +35,7 @@ import com.iluwatar.layers.entity.CakeTopping;
import com.iluwatar.layers.exception.CakeBakingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.context.support.AbstractApplicationContext;
@ -72,7 +70,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
Set<CakeLayer> foundLayers = new HashSet<>();
for (var info : cakeInfo.cakeLayerInfos) {
var found = allLayers.stream().filter(layer -> layer.getName().equals(info.name)).findFirst();
if (!found.isPresent()) {
if (found.isEmpty()) {
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
} else {
foundLayers.add(found.get());
@ -114,9 +112,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
private List<CakeTopping> getAvailableToppingEntities() {
var bean = context.getBean(CakeToppingDao.class);
List<CakeTopping> result = new ArrayList<>();
var iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
var topping = iterator.next();
for (CakeTopping topping : bean.findAll()) {
if (topping.getCake() == null) {
result.add(topping);
}
@ -128,9 +124,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
public List<CakeToppingInfo> getAvailableToppings() {
var bean = context.getBean(CakeToppingDao.class);
List<CakeToppingInfo> result = new ArrayList<>();
var iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
var next = iterator.next();
for (CakeTopping next : bean.findAll()) {
if (next.getCake() == null) {
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
}
@ -141,9 +135,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
private List<CakeLayer> getAvailableLayerEntities() {
var bean = context.getBean(CakeLayerDao.class);
List<CakeLayer> result = new ArrayList<>();
var iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
var next = iterator.next();
for (CakeLayer next : bean.findAll()) {
if (next.getCake() == null) {
result.add(next);
}
@ -155,9 +147,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
public List<CakeLayerInfo> getAvailableLayers() {
var bean = context.getBean(CakeLayerDao.class);
List<CakeLayerInfo> result = new ArrayList<>();
var iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
var next = iterator.next();
for (CakeLayer next : bean.findAll()) {
if (next.getCake() == null) {
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
}
@ -169,9 +159,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
public List<CakeInfo> getAllCakes() {
var cakeBean = context.getBean(CakeDao.class);
List<CakeInfo> result = new ArrayList<>();
var iterator = cakeBean.findAll().iterator();
while (iterator.hasNext()) {
var cake = iterator.next();
for (Cake cake : cakeBean.findAll()) {
var cakeToppingInfo =
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake
.getTopping().getCalories());