#1021 Checkstyle fixes for Layers
This commit is contained in:
parent
47ae477a56
commit
8ecdee44c8
@ -26,32 +26,32 @@ package com.iluwatar.layers;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* 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.
|
||||
* <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.
|
||||
* <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.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* <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.
|
||||
*
|
||||
* <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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* @see Cake
|
||||
* @see CakeTopping
|
||||
@ -70,7 +70,7 @@ public class App {
|
||||
private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl();
|
||||
|
||||
/**
|
||||
* Application entry point
|
||||
* Application entry point.
|
||||
*
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
@ -85,7 +85,7 @@ public class App {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the example data
|
||||
* Initializes the example data.
|
||||
*/
|
||||
private static void initializeData(CakeBakingService cakeBakingService) {
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||
@ -108,10 +108,10 @@ public class App {
|
||||
} catch (CakeBakingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
CakeInfo cake2 =
|
||||
new CakeInfo(new CakeToppingInfo("cherry", 0), List.of(
|
||||
new CakeLayerInfo("vanilla", 0), new CakeLayerInfo("lemon", 0), new CakeLayerInfo(
|
||||
"strawberry", 0)));
|
||||
CakeInfo cake2 = new CakeInfo(new CakeToppingInfo("cherry", 0), List.of(
|
||||
new CakeLayerInfo("vanilla", 0),
|
||||
new CakeLayerInfo("lemon", 0),
|
||||
new CakeLayerInfo("strawberry", 0)));
|
||||
try {
|
||||
cakeBakingService.bakeNewCake(cake2);
|
||||
} catch (CakeBakingException e) {
|
||||
|
@ -35,9 +35,7 @@ import javax.persistence.OneToMany;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
/**
|
||||
*
|
||||
* Cake entity
|
||||
*
|
||||
* Cake entity.
|
||||
*/
|
||||
@Entity
|
||||
public class Cake {
|
||||
|
@ -24,15 +24,14 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
/**
|
||||
*
|
||||
* Custom exception used in cake baking
|
||||
*
|
||||
* Custom exception used in cake baking.
|
||||
*/
|
||||
public class CakeBakingException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CakeBakingException() {}
|
||||
public CakeBakingException() {
|
||||
}
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
|
@ -26,39 +26,37 @@ package com.iluwatar.layers;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* Service for cake baking operations
|
||||
*
|
||||
* Service for cake baking operations.
|
||||
*/
|
||||
public interface CakeBakingService {
|
||||
|
||||
/**
|
||||
* Bakes new cake according to parameters
|
||||
* Bakes new cake according to parameters.
|
||||
*/
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
/**
|
||||
* Get all cakes
|
||||
* Get all cakes.
|
||||
*/
|
||||
List<CakeInfo> getAllCakes();
|
||||
|
||||
/**
|
||||
* Store new cake topping
|
||||
* Store new cake topping.
|
||||
*/
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
/**
|
||||
* Get available cake toppings
|
||||
* Get available cake toppings.
|
||||
*/
|
||||
List<CakeToppingInfo> getAvailableToppings();
|
||||
|
||||
/**
|
||||
* Add new cake layer
|
||||
* Add new cake layer.
|
||||
*/
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
/**
|
||||
* Get available cake layers
|
||||
* Get available cake layers.
|
||||
*/
|
||||
List<CakeLayerInfo> getAvailableLayers();
|
||||
}
|
||||
|
@ -37,9 +37,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
*
|
||||
* Implementation of CakeBakingService
|
||||
*
|
||||
* Implementation of CakeBakingService.
|
||||
*/
|
||||
@Service
|
||||
@Transactional
|
||||
@ -73,7 +71,8 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
}
|
||||
}
|
||||
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
Optional<CakeTopping> topping = toppingBean.findById(matchingToppings.iterator().next().getId());
|
||||
Optional<CakeTopping> topping = toppingBean.findById(
|
||||
matchingToppings.iterator().next().getId());
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
if (topping.isPresent()) {
|
||||
Cake cake = new Cake();
|
||||
|
@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* CRUD repository for cakes
|
||||
*
|
||||
* CRUD repository for cakes.
|
||||
*/
|
||||
@Repository
|
||||
public interface CakeDao extends CrudRepository<Cake, Long> {
|
||||
|
@ -27,9 +27,7 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* DTO for cakes
|
||||
*
|
||||
* DTO for cakes.
|
||||
*/
|
||||
public class CakeInfo {
|
||||
|
||||
@ -38,7 +36,7 @@ public class CakeInfo {
|
||||
public final List<CakeLayerInfo> cakeLayerInfos;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.of(id);
|
||||
@ -47,7 +45,7 @@ public class CakeInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.empty();
|
||||
@ -56,7 +54,7 @@ public class CakeInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate calories
|
||||
* Calculate calories.
|
||||
*/
|
||||
public int calculateTotalCalories() {
|
||||
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
|
||||
|
@ -30,9 +30,7 @@ import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
/**
|
||||
*
|
||||
* CakeLayer entity
|
||||
*
|
||||
* CakeLayer entity.
|
||||
*/
|
||||
@Entity
|
||||
public class CakeLayer {
|
||||
@ -48,7 +46,8 @@ public class CakeLayer {
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeLayer() {}
|
||||
public CakeLayer() {
|
||||
}
|
||||
|
||||
public CakeLayer(String name, int calories) {
|
||||
this.setName(name);
|
||||
|
@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* CRUD repository for cake layers
|
||||
*
|
||||
* CRUD repository for cake layers.
|
||||
*/
|
||||
@Repository
|
||||
public interface CakeLayerDao extends CrudRepository<CakeLayer, Long> {
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.layers;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* DTO for cake layers
|
||||
*
|
||||
* DTO for cake layers.
|
||||
*/
|
||||
public class CakeLayerInfo {
|
||||
|
||||
@ -37,7 +35,7 @@ public class CakeLayerInfo {
|
||||
public final int calories;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeLayerInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
@ -46,7 +44,7 @@ public class CakeLayerInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeLayerInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
|
@ -30,9 +30,7 @@ import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
/**
|
||||
*
|
||||
* CakeTopping entity
|
||||
*
|
||||
* CakeTopping entity.
|
||||
*/
|
||||
@Entity
|
||||
public class CakeTopping {
|
||||
@ -48,7 +46,8 @@ public class CakeTopping {
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeTopping() {}
|
||||
public CakeTopping() {
|
||||
}
|
||||
|
||||
public CakeTopping(String name, int calories) {
|
||||
this.setName(name);
|
||||
|
@ -27,9 +27,7 @@ import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
*
|
||||
* CRUD repository cake toppings
|
||||
*
|
||||
* CRUD repository cake toppings.
|
||||
*/
|
||||
@Repository
|
||||
public interface CakeToppingDao extends CrudRepository<CakeTopping, Long> {
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.layers;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* DTO for cake toppings
|
||||
*
|
||||
* DTO for cake toppings.
|
||||
*/
|
||||
public class CakeToppingInfo {
|
||||
|
||||
@ -37,7 +35,7 @@ public class CakeToppingInfo {
|
||||
public final int calories;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeToppingInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
@ -46,7 +44,7 @@ public class CakeToppingInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Constructor.
|
||||
*/
|
||||
public CakeToppingInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
@ -56,6 +54,7 @@ public class CakeToppingInfo {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories);
|
||||
return String.format("CakeToppingInfo id=%d name=%s calories=%d",
|
||||
id.orElse(-1L), name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -27,9 +27,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* View implementation for displaying cakes
|
||||
*
|
||||
* View implementation for displaying cakes.
|
||||
*/
|
||||
public class CakeViewImpl implements View {
|
||||
|
||||
|
@ -24,9 +24,7 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
/**
|
||||
*
|
||||
* View interface
|
||||
*
|
||||
* View interface.
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user