#590 arrange Layers into packages and add explanation
This commit is contained in:
@ -12,7 +12,7 @@ tags:
|
||||
---
|
||||
|
||||
## Intent
|
||||
Layers is an architectural style where software responsibilities are
|
||||
Layers is an architectural pattern where software responsibilities are
|
||||
divided among the different layers of the application.
|
||||
|
||||

|
||||
@ -24,6 +24,84 @@ Use the Layers architecture when
|
||||
* you want to prevent a change from propagating throughout the application
|
||||
* you want to make your application more maintainable and testable
|
||||
|
||||
## 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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
On the data layer, we keep our cake building blocks. Cakes consist of layers and topping.
|
||||
|
||||
```java
|
||||
@Entity
|
||||
public class Cake {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.REMOVE)
|
||||
private CakeTopping topping;
|
||||
|
||||
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
|
||||
private Set<CakeLayer> layers;
|
||||
}
|
||||
```
|
||||
|
||||
The service layer offers CakeBakingService for easy access to different aspects of cakes.
|
||||
|
||||
```java
|
||||
public interface CakeBakingService {
|
||||
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
List<CakeInfo> getAllCakes();
|
||||
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
List<CakeToppingInfo> getAvailableToppings();
|
||||
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
List<CakeLayerInfo> getAvailableLayers();
|
||||
}
|
||||
```
|
||||
|
||||
On the top we have our view responsible of rendering the cakes.
|
||||
|
||||
```java
|
||||
public interface View {
|
||||
|
||||
void render();
|
||||
|
||||
}
|
||||
|
||||
public class CakeViewImpl implements View {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CakeViewImpl.class);
|
||||
|
||||
private CakeBakingService cakeBakingService;
|
||||
|
||||
public CakeViewImpl(CakeBakingService cakeBakingService) {
|
||||
this.cakeBakingService = cakeBakingService;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
cakeBakingService.getAllCakes().forEach(cake -> LOGGER.info(cake.toString()));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Credits
|
||||
|
||||
* [Pattern Oriented Software Architecture Vol I-V](http://www.amazon.com/Pattern-Oriented-Software-Architecture-Volume-Patterns/dp/0471958697)
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<interface id="1" language="java" name="com.iluwatar.layers.CakeDao" project="layers"
|
||||
<interface id="1" language="java" name="com.iluwatar.layers.dao.CakeDao" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeDao.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="289" y="916"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -10,7 +10,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="2" language="java" name="com.iluwatar.layers.CakeLayer" project="layers"
|
||||
<class id="2" language="java" name="com.iluwatar.layers.entity.CakeLayer" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeLayer.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1438" y="826"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -19,7 +19,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="3" language="java" name="com.iluwatar.layers.CakeViewImpl" project="layers"
|
||||
<class id="3" language="java" name="com.iluwatar.layers.view.CakeViewImpl" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeViewImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="456" y="221"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -28,7 +28,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.layers.CakeBakingException" project="layers"
|
||||
<class id="4" language="java" name="com.iluwatar.layers.exception.CakeBakingException" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeBakingException.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="143" y="502"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -37,7 +37,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.layers.CakeBakingServiceImpl" project="layers"
|
||||
<class id="5" language="java" name="com.iluwatar.layers.service.CakeBakingServiceImpl" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeBakingServiceImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="456" y="694"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -46,7 +46,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="6" language="java" name="com.iluwatar.layers.CakeLayerDao" project="layers"
|
||||
<interface id="6" language="java" name="com.iluwatar.layers.dao.CakeLayerDao" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeLayerDao.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="456" y="918"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -55,7 +55,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="7" language="java" name="com.iluwatar.layers.View" project="layers"
|
||||
<interface id="7" language="java" name="com.iluwatar.layers.view.View" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/View.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="456" y="65"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -64,7 +64,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="8" language="java" name="com.iluwatar.layers.CakeToppingInfo" project="layers"
|
||||
<class id="8" language="java" name="com.iluwatar.layers.dto.CakeToppingInfo" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeToppingInfo.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="817" y="530"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -73,7 +73,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="9" language="java" name="com.iluwatar.layers.CakeInfo" project="layers"
|
||||
<class id="9" language="java" name="com.iluwatar.layers.dto.CakeInfo" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeInfo.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="883" y="265"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -82,7 +82,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="10" language="java" name="com.iluwatar.layers.CakeToppingDao" project="layers"
|
||||
<interface id="10" language="java" name="com.iluwatar.layers.dao.CakeToppingDao" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeToppingDao.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="633" y="918"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -91,7 +91,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<interface id="11" language="java" name="com.iluwatar.layers.CakeBakingService" project="layers"
|
||||
<interface id="11" language="java" name="com.iluwatar.layers.service.CakeBakingService" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeBakingService.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="456" y="431"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -100,7 +100,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="12" language="java" name="com.iluwatar.layers.CakeLayerInfo" project="layers"
|
||||
<class id="12" language="java" name="com.iluwatar.layers.dto.CakeLayerInfo" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeLayerInfo.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1055" y="530"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -109,7 +109,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="13" language="java" name="com.iluwatar.layers.Cake" project="layers"
|
||||
<class id="13" language="java" name="com.iluwatar.layers.entity.Cake" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/Cake.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1160" y="826"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
@ -118,7 +118,7 @@
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="14" language="java" name="com.iluwatar.layers.CakeTopping" project="layers"
|
||||
<class id="14" language="java" name="com.iluwatar.layers.entity.CakeTopping" project="layers"
|
||||
file="/layers/src/main/java/com/iluwatar/layers/CakeTopping.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="876" y="826"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
|
@ -21,8 +21,21 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.app;
|
||||
|
||||
import com.iluwatar.layers.dao.CakeDao;
|
||||
import com.iluwatar.layers.dao.CakeLayerDao;
|
||||
import com.iluwatar.layers.dao.CakeToppingDao;
|
||||
import com.iluwatar.layers.dto.CakeInfo;
|
||||
import com.iluwatar.layers.dto.CakeLayerInfo;
|
||||
import com.iluwatar.layers.dto.CakeToppingInfo;
|
||||
import com.iluwatar.layers.entity.Cake;
|
||||
import com.iluwatar.layers.entity.CakeLayer;
|
||||
import com.iluwatar.layers.entity.CakeTopping;
|
||||
import com.iluwatar.layers.exception.CakeBakingException;
|
||||
import com.iluwatar.layers.service.CakeBakingService;
|
||||
import com.iluwatar.layers.service.CakeBakingServiceImpl;
|
||||
import com.iluwatar.layers.view.CakeViewImpl;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dao;
|
||||
|
||||
import com.iluwatar.layers.entity.Cake;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dao;
|
||||
|
||||
import com.iluwatar.layers.entity.CakeLayer;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dao;
|
||||
|
||||
import com.iluwatar.layers.entity.CakeTopping;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dto;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dto;
|
||||
|
||||
import java.util.Optional;
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.dto;
|
||||
|
||||
import java.util.Optional;
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.entity;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@ -74,7 +74,7 @@ public class Cake {
|
||||
return layers;
|
||||
}
|
||||
|
||||
public final void setLayers(Set<CakeLayer> layers) {
|
||||
public void setLayers(Set<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.entity;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
@ -66,7 +66,7 @@ public class CakeLayer {
|
||||
return name;
|
||||
}
|
||||
|
||||
public final void setName(String name) {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@ -74,7 +74,7 @@ public class CakeLayer {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public final void setCalories(int calories) {
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.entity;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
@ -66,11 +66,11 @@ public class CakeTopping {
|
||||
return name;
|
||||
}
|
||||
|
||||
public final void setName(String name) {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public final int getCalories() {
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.exception;
|
||||
|
||||
/**
|
||||
* Custom exception used in cake baking.
|
@ -21,8 +21,12 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.service;
|
||||
|
||||
import com.iluwatar.layers.dto.CakeInfo;
|
||||
import com.iluwatar.layers.dto.CakeLayerInfo;
|
||||
import com.iluwatar.layers.dto.CakeToppingInfo;
|
||||
import com.iluwatar.layers.exception.CakeBakingException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
@ -21,8 +21,18 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.service;
|
||||
|
||||
import com.iluwatar.layers.dto.CakeInfo;
|
||||
import com.iluwatar.layers.dto.CakeLayerInfo;
|
||||
import com.iluwatar.layers.dto.CakeToppingInfo;
|
||||
import com.iluwatar.layers.dao.CakeDao;
|
||||
import com.iluwatar.layers.dao.CakeLayerDao;
|
||||
import com.iluwatar.layers.dao.CakeToppingDao;
|
||||
import com.iluwatar.layers.entity.Cake;
|
||||
import com.iluwatar.layers.entity.CakeLayer;
|
||||
import com.iluwatar.layers.entity.CakeTopping;
|
||||
import com.iluwatar.layers.exception.CakeBakingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.view;
|
||||
|
||||
import com.iluwatar.layers.service.CakeBakingService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -21,7 +21,7 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.view;
|
||||
|
||||
/**
|
||||
* View interface.
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.app;
|
||||
|
||||
import com.iluwatar.layers.app.App;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
@ -21,8 +21,11 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.entity;
|
||||
|
||||
import com.iluwatar.layers.entity.Cake;
|
||||
import com.iluwatar.layers.entity.CakeLayer;
|
||||
import com.iluwatar.layers.entity.CakeTopping;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
@ -21,8 +21,9 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.exception;
|
||||
|
||||
import com.iluwatar.layers.exception.CakeBakingException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
@ -21,8 +21,13 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.service;
|
||||
|
||||
import com.iluwatar.layers.dto.CakeInfo;
|
||||
import com.iluwatar.layers.dto.CakeLayerInfo;
|
||||
import com.iluwatar.layers.dto.CakeToppingInfo;
|
||||
import com.iluwatar.layers.exception.CakeBakingException;
|
||||
import com.iluwatar.layers.service.CakeBakingServiceImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
@ -21,11 +21,16 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.layers;
|
||||
package com.iluwatar.layers.view;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import com.iluwatar.layers.dto.CakeInfo;
|
||||
import com.iluwatar.layers.dto.CakeLayerInfo;
|
||||
import com.iluwatar.layers.dto.CakeToppingInfo;
|
||||
import com.iluwatar.layers.service.CakeBakingService;
|
||||
import com.iluwatar.layers.view.CakeViewImpl;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
Reference in New Issue
Block a user