Skip to content

Commit db9a00e

Browse files
committed
iluwatar#84 Documented the Layers example
1 parent 9aa831d commit db9a00e

15 files changed

+158
-0
lines changed

layers/src/main/java/com/iluwatar/layers/App.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,67 @@
22

33
import java.util.Arrays;
44

5+
/**
6+
*
7+
* <p>
8+
* Multilayered architecture is an architectural style where software responsibilities are
9+
* divided among the different layers of the application.
10+
* </p>
11+
*
12+
* <p>
13+
* This example demonstrates a traditional 3-layer architecture consisting of data access
14+
* layer, business layer and presentation layer.
15+
* </p>
16+
*
17+
* <p>
18+
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>, <code>CakeToppingDao</code> and
19+
* <code>CakeLayerDao</code>. The repositories can be used for CRUD operations on cakes, cake toppings
20+
* and cake layers respectively.
21+
* </p>
22+
*
23+
* <p>
24+
* The business layer is built on top of the data access layer. <code>CakeBakingService</code> offers
25+
* methods to retrieve available cake toppings and cake layers and baked cakes. Also the
26+
* service is used to create new cakes out of cake toppings and cake layers.
27+
* </p>
28+
*
29+
* <p>
30+
* The presentation layer is built on the business layer and in this example it simply lists
31+
* the cakes that have been baked.
32+
* </p>
33+
*
34+
* <p>
35+
* We have applied so called strict layering which means that the layers can only access
36+
* the classes directly beneath them. This leads the solution to create an additional set of
37+
* DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
38+
* to translate data between layers. In other words, <code>CakeBakingService</code> cannot
39+
* return entities (<code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>)
40+
* directly since these reside on data access layer but instead translates these into business
41+
* layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
42+
* and returns them instead. This way the presentation layer does not have any knowledge of
43+
* other layers than the business layer and thus is not affected by changes to them.
44+
* </p>
45+
*
46+
* @see Cake
47+
* @see CakeTopping
48+
* @see CakeLayer
49+
* @see CakeDao
50+
* @see CakeToppingDao
51+
* @see CakeLayerDao
52+
* @see CakeBakingService
53+
* @see CakeInfo
54+
* @see CakeToppingInfo
55+
* @see CakeLayerInfo
56+
*
57+
*/
558
public class App {
659

760
private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl();
861

62+
/**
63+
* Application entry point
64+
* @param args Command line parameters
65+
*/
966
public static void main(String[] args) {
1067

1168
// initialize example data
@@ -16,6 +73,10 @@ public static void main(String[] args) {
1673
cakeView.render();
1774
}
1875

76+
/**
77+
* Initializes the example data
78+
* @param cakeBakingService
79+
*/
1980
private static void initializeData(CakeBakingService cakeBakingService) {
2081
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
2182
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));

layers/src/main/java/com/iluwatar/layers/Cake.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
import javax.persistence.OneToMany;
1212
import javax.persistence.OneToOne;
1313

14+
/**
15+
*
16+
* Cake entity
17+
*
18+
*/
1419
@Entity
1520
public class Cake {
1621

layers/src/main/java/com/iluwatar/layers/CakeBakingException.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.iluwatar.layers;
22

3+
/**
4+
*
5+
* Custom exception used in cake baking
6+
*
7+
*/
38
public class CakeBakingException extends Exception {
49

10+
private static final long serialVersionUID = 1L;
11+
512
public CakeBakingException() {
613
}
714

layers/src/main/java/com/iluwatar/layers/CakeBakingService.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,47 @@
22

33
import java.util.List;
44

5+
/**
6+
*
7+
* Service for cake baking operations
8+
*
9+
*/
510
public interface CakeBakingService {
611

12+
/**
13+
* Bakes new cake according to parameters
14+
* @param cakeInfo
15+
* @throws CakeBakingException
16+
*/
717
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
818

19+
/**
20+
* Get all cakes
21+
* @return
22+
*/
923
List<CakeInfo> getAllCakes();
1024

25+
/**
26+
* Store new cake topping
27+
* @param toppingInfo
28+
*/
1129
void saveNewTopping(CakeToppingInfo toppingInfo);
1230

31+
/**
32+
* Get available cake toppings
33+
* @return
34+
*/
1335
List<CakeToppingInfo> getAvailableToppings();
1436

37+
/**
38+
* Add new cake layer
39+
* @param layerInfo
40+
*/
1541
void saveNewLayer(CakeLayerInfo layerInfo);
1642

43+
/**
44+
* Get available cake layers
45+
* @return
46+
*/
1747
List<CakeLayerInfo> getAvailableLayers();
1848
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
import org.springframework.stereotype.Service;
1414
import org.springframework.transaction.annotation.Transactional;
1515

16+
/**
17+
*
18+
* Implementation of CakeBakingService
19+
*
20+
*/
1621
@Service
1722
@Transactional
1823
public class CakeBakingServiceImpl implements CakeBakingService {

layers/src/main/java/com/iluwatar/layers/CakeDao.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import org.springframework.data.repository.CrudRepository;
44
import org.springframework.stereotype.Repository;
55

6+
/**
7+
*
8+
* CRUD repository for cakes
9+
*
10+
*/
611
@Repository
712
public interface CakeDao extends CrudRepository<Cake, Long> {
813

layers/src/main/java/com/iluwatar/layers/CakeInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import java.util.List;
44
import java.util.Optional;
55

6+
/**
7+
*
8+
* DTO for cakes
9+
*
10+
*/
611
public class CakeInfo {
712

813
public final Optional<Long> id;

layers/src/main/java/com/iluwatar/layers/CakeLayer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import javax.persistence.Id;
77
import javax.persistence.ManyToOne;
88

9+
/**
10+
*
11+
* CakeLayer entity
12+
*
13+
*/
914
@Entity
1015
public class CakeLayer {
1116

layers/src/main/java/com/iluwatar/layers/CakeLayerDao.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import org.springframework.data.repository.CrudRepository;
44
import org.springframework.stereotype.Repository;
55

6+
/**
7+
*
8+
* CRUD repository for cake layers
9+
*
10+
*/
611
@Repository
712
public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> {
813

layers/src/main/java/com/iluwatar/layers/CakeLayerInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import java.util.Optional;
44

5+
/**
6+
*
7+
* DTO for cake layers
8+
*
9+
*/
510
public class CakeLayerInfo {
611

712
public final Optional<Long> id;

0 commit comments

Comments
 (0)