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 ## 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 ## Explanation
Real world example 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 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** **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 ```java
@Entity @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 ```java
public interface CakeBakingService { 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 ```java
public interface View { public interface View {
@ -92,14 +99,16 @@ public class CakeViewImpl implements View {
``` ```
## Class diagram ## Class diagram
![alt text](./etc/layers.png "Layers") ![alt text](./etc/layers.png "Layers")
## Applicability ## Applicability
Use the Layers architecture when Use the Layers architecture when
* you want clearly divide software responsibilities into different parts of the program * 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 prevent a change from propagating throughout the application.
* you want to make your application more maintainable and testable * You want to make your application more maintainable and testable.
## Credits ## Credits

View File

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