mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 02:59:21 +00:00
Adjust checkstyle rules. Make checkstyle fail the build when violations are found. Correct all current checkstyle violations.
This commit is contained in:
@@ -4,31 +4,31 @@ import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* Layers is an architectural style where software responsibilities are divided among the different
|
||||
* layers of the application.
|
||||
* Layers is an architectural style where software responsibilities are divided among the different layers of the
|
||||
* application.
|
||||
* <p>
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access layer,
|
||||
* business layer and presentation layer.
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access layer, business layer and
|
||||
* presentation layer.
|
||||
* <p>
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>,
|
||||
* <code>CakeToppingDao</code> and <code>CakeLayerDao</code>. The repositories can be used for CRUD
|
||||
* operations on cakes, cake toppings and cake layers respectively.
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>, <code>CakeToppingDao</code> and
|
||||
* <code>CakeLayerDao</code>. The repositories can be used for CRUD operations on cakes, cake toppings and cake layers
|
||||
* respectively.
|
||||
* <p>
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code>
|
||||
* offers methods to retrieve available cake toppings and cake layers and baked cakes. Also the
|
||||
* service is used to create new cakes out of cake toppings and cake layers.
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code> offers methods to
|
||||
* retrieve available cake toppings and cake layers and baked cakes. Also the service is used to create new cakes out of
|
||||
* cake toppings and cake layers.
|
||||
* <p>
|
||||
* The presentation layer is built on the business layer and in this example it simply lists the
|
||||
* cakes that have been baked.
|
||||
* The presentation layer is built on the business layer and in this example it simply lists the cakes that have been
|
||||
* baked.
|
||||
* <p>
|
||||
* We have applied so called strict layering which means that the layers can only access the classes
|
||||
* directly beneath them. This leads the solution to create an additional set of DTOs (
|
||||
* <code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate
|
||||
* data between layers. In other words, <code>CakeBakingService</code> cannot return entities (
|
||||
* <code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>) directly since these reside
|
||||
* on data access layer but instead translates these into business layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns them instead. This way
|
||||
* the presentation layer does not have any knowledge of other layers than the business layer and
|
||||
* thus is not affected by changes to them.
|
||||
* We have applied so called strict layering which means that the layers can only access the classes directly beneath
|
||||
* them. This leads the solution to create an additional set of DTOs ( <code>CakeInfo</code>,
|
||||
* <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate data between layers. In other words,
|
||||
* <code>CakeBakingService</code> cannot return entities ( <code>Cake</code>, <code>CakeTopping</code>,
|
||||
* <code>CakeLayer</code>) directly since these reside on data access layer but instead translates these into business
|
||||
* layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns them
|
||||
* instead. This way the presentation layer does not have any knowledge of other layers than the business layer and thus
|
||||
* is not affected by changes to them.
|
||||
*
|
||||
* @see Cake
|
||||
* @see CakeTopping
|
||||
@@ -63,8 +63,6 @@ public class App {
|
||||
|
||||
/**
|
||||
* Initializes the example data
|
||||
*
|
||||
* @param cakeBakingService
|
||||
*/
|
||||
private static void initializeData(CakeBakingService cakeBakingService) {
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||
|
||||
@@ -11,44 +11,31 @@ public interface CakeBakingService {
|
||||
|
||||
/**
|
||||
* Bakes new cake according to parameters
|
||||
*
|
||||
* @param cakeInfo
|
||||
* @throws CakeBakingException
|
||||
*/
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
/**
|
||||
* Get all cakes
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeInfo> getAllCakes();
|
||||
|
||||
/**
|
||||
* Store new cake topping
|
||||
*
|
||||
* @param toppingInfo
|
||||
*/
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
/**
|
||||
* Get available cake toppings
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeToppingInfo> getAvailableToppings();
|
||||
|
||||
/**
|
||||
* Add new cake layer
|
||||
*
|
||||
* @param layerInfo
|
||||
*/
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
/**
|
||||
* Get available cake layers
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeLayerInfo> getAvailableLayers();
|
||||
}
|
||||
|
||||
@@ -14,18 +14,27 @@ public class CakeInfo {
|
||||
public final CakeToppingInfo cakeToppingInfo;
|
||||
public final List<CakeLayerInfo> cakeLayerInfos;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.of(id);
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.empty();
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate calories
|
||||
*/
|
||||
public int calculateTotalCalories() {
|
||||
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
|
||||
total += cakeLayerInfos.stream().mapToInt(c -> c.calories).sum();
|
||||
|
||||
@@ -13,12 +13,18 @@ public class CakeLayerInfo {
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeLayerInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeLayerInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
|
||||
@@ -13,12 +13,18 @@ public class CakeToppingInfo {
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeToppingInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CakeToppingInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
|
||||
Reference in New Issue
Block a user