Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 29, 2020
1 parent bcca9be commit 3544a83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
33 changes: 21 additions & 12 deletions layers/README.md
Expand Up @@ -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 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**

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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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

Expand Down
Expand Up @@ -35,9 +35,7 @@
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;
Expand Down Expand Up @@ -72,7 +70,7 @@ public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
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());
Expand Down Expand Up @@ -114,9 +112,7 @@ public void saveNewLayer(CakeLayerInfo layerInfo) {
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);
}
Expand All @@ -128,9 +124,7 @@ private List<CakeTopping> getAvailableToppingEntities() {
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()));
}
Expand All @@ -141,9 +135,7 @@ public List<CakeToppingInfo> getAvailableToppings() {
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);
}
Expand All @@ -155,9 +147,7 @@ private List<CakeLayer> getAvailableLayerEntities() {
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()));
}
Expand All @@ -169,9 +159,7 @@ public List<CakeLayerInfo> getAvailableLayers() {
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());
Expand Down

0 comments on commit 3544a83

Please sign in to comment.