Skip to content

Commit 3544a83

Browse files
committed
Update README.md
1 parent bcca9be commit 3544a83

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

layers/README.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,33 @@ tags:
1010
---
1111

1212
## Intent
13-
Layers is an architectural pattern where software responsibilities are
14-
divided among the different layers of the application.
13+
14+
Layers is an architectural pattern where software responsibilities are divided among the different
15+
layers of the application.
1516

1617
## Explanation
1718

1819
Real world example
1920

20-
> 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.
21+
> Consider a web site displaying decorated cakes for weddings and such. Instead of the web page
22+
> directly reaching into the database, it relies on a service to deliver this information. The
23+
> service then queries the data layer to assimilate the needed information.
2124
22-
In Plain Words
25+
In plain words
2326

24-
> 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.
27+
> With Layers architectural pattern different concerns reside on separate layers. View layer is
28+
> interested only in rendering, service layer assembles the requested data from various sources, and
29+
> data layer gets the bits from the data storage.
2530
2631
Wikipedia says
2732

28-
> 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.
33+
> In software engineering, multitier architecture (often referred to as n-tier architecture) or
34+
> multilayered architecture is a client–server architecture in which presentation, application
35+
> processing, and data management functions are physically separated.
2936
3037
**Programmatic Example**
3138

32-
On the data layer, we keep our cake building blocks. Cakes consist of layers and topping.
39+
On the data layer, we keep our cake building blocks. `Cake` consist of layers and topping.
3340

3441
```java
3542
@Entity
@@ -47,7 +54,7 @@ public class Cake {
4754
}
4855
```
4956

50-
The service layer offers CakeBakingService for easy access to different aspects of cakes.
57+
The service layer offers `CakeBakingService` for easy access to different aspects of cakes.
5158

5259
```java
5360
public interface CakeBakingService {
@@ -66,7 +73,7 @@ public interface CakeBakingService {
6673
}
6774
```
6875

69-
On the top we have our view responsible of rendering the cakes.
76+
On the top we have our `View` responsible of rendering the cakes.
7077

7178
```java
7279
public interface View {
@@ -92,14 +99,16 @@ public class CakeViewImpl implements View {
9299
```
93100

94101
## Class diagram
102+
95103
![alt text](./etc/layers.png "Layers")
96104

97105
## Applicability
106+
98107
Use the Layers architecture when
99108

100-
* you want clearly divide software responsibilities into different parts of the program
101-
* you want to prevent a change from propagating throughout the application
102-
* you want to make your application more maintainable and testable
109+
* You want clearly divide software responsibilities into different parts of the program.
110+
* You want to prevent a change from propagating throughout the application.
111+
* You want to make your application more maintainable and testable.
103112

104113
## Credits
105114

layers/src/main/java/com/iluwatar/layers/service/CakeBakingServiceImpl.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
import com.iluwatar.layers.exception.CakeBakingException;
3636
import java.util.ArrayList;
3737
import java.util.HashSet;
38-
import java.util.Iterator;
3938
import java.util.List;
40-
import java.util.Optional;
4139
import java.util.Set;
4240
import java.util.stream.Collectors;
4341
import org.springframework.context.support.AbstractApplicationContext;
@@ -72,7 +70,7 @@ public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
7270
Set<CakeLayer> foundLayers = new HashSet<>();
7371
for (var info : cakeInfo.cakeLayerInfos) {
7472
var found = allLayers.stream().filter(layer -> layer.getName().equals(info.name)).findFirst();
75-
if (!found.isPresent()) {
73+
if (found.isEmpty()) {
7674
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
7775
} else {
7876
foundLayers.add(found.get());
@@ -114,9 +112,7 @@ public void saveNewLayer(CakeLayerInfo layerInfo) {
114112
private List<CakeTopping> getAvailableToppingEntities() {
115113
var bean = context.getBean(CakeToppingDao.class);
116114
List<CakeTopping> result = new ArrayList<>();
117-
var iterator = bean.findAll().iterator();
118-
while (iterator.hasNext()) {
119-
var topping = iterator.next();
115+
for (CakeTopping topping : bean.findAll()) {
120116
if (topping.getCake() == null) {
121117
result.add(topping);
122118
}
@@ -128,9 +124,7 @@ private List<CakeTopping> getAvailableToppingEntities() {
128124
public List<CakeToppingInfo> getAvailableToppings() {
129125
var bean = context.getBean(CakeToppingDao.class);
130126
List<CakeToppingInfo> result = new ArrayList<>();
131-
var iterator = bean.findAll().iterator();
132-
while (iterator.hasNext()) {
133-
var next = iterator.next();
127+
for (CakeTopping next : bean.findAll()) {
134128
if (next.getCake() == null) {
135129
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
136130
}
@@ -141,9 +135,7 @@ public List<CakeToppingInfo> getAvailableToppings() {
141135
private List<CakeLayer> getAvailableLayerEntities() {
142136
var bean = context.getBean(CakeLayerDao.class);
143137
List<CakeLayer> result = new ArrayList<>();
144-
var iterator = bean.findAll().iterator();
145-
while (iterator.hasNext()) {
146-
var next = iterator.next();
138+
for (CakeLayer next : bean.findAll()) {
147139
if (next.getCake() == null) {
148140
result.add(next);
149141
}
@@ -155,9 +147,7 @@ private List<CakeLayer> getAvailableLayerEntities() {
155147
public List<CakeLayerInfo> getAvailableLayers() {
156148
var bean = context.getBean(CakeLayerDao.class);
157149
List<CakeLayerInfo> result = new ArrayList<>();
158-
var iterator = bean.findAll().iterator();
159-
while (iterator.hasNext()) {
160-
var next = iterator.next();
150+
for (CakeLayer next : bean.findAll()) {
161151
if (next.getCake() == null) {
162152
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
163153
}
@@ -169,9 +159,7 @@ public List<CakeLayerInfo> getAvailableLayers() {
169159
public List<CakeInfo> getAllCakes() {
170160
var cakeBean = context.getBean(CakeDao.class);
171161
List<CakeInfo> result = new ArrayList<>();
172-
var iterator = cakeBean.findAll().iterator();
173-
while (iterator.hasNext()) {
174-
var cake = iterator.next();
162+
for (Cake cake : cakeBean.findAll()) {
175163
var cakeToppingInfo =
176164
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake
177165
.getTopping().getCalories());

0 commit comments

Comments
 (0)