Skip to content

Commit

Permalink
#590 arrange Layers into packages and add explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Nov 5, 2019
1 parent 8ecdee4 commit 50986fa
Show file tree
Hide file tree
Showing 22 changed files with 164 additions and 40 deletions.
80 changes: 79 additions & 1 deletion layers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

![alt text](./etc/layers.png "Layers")
Expand All @@ -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)
28 changes: 14 additions & 14 deletions layers/etc/layers.ucls
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.dto;

import java.util.List;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.dto;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.dto;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.entity;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -74,7 +74,7 @@ public Set<CakeLayer> getLayers() {
return layers;
}

public final void setLayers(Set<CakeLayer> layers) {
public void setLayers(Set<CakeLayer> layers) {
this.layers = layers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
Expand Down Expand Up @@ -66,15 +66,15 @@ public String getName() {
return name;
}

public final void setName(String name) {
public void setName(String name) {
this.name = name;
}

public int getCalories() {
return calories;
}

public final void setCalories(int calories) {
public void setCalories(int calories) {
this.calories = calories;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
Expand Down Expand Up @@ -66,11 +66,11 @@ public String getName() {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

package com.iluwatar.layers;
package com.iluwatar.layers.exception;

/**
* Custom exception used in cake baking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down

0 comments on commit 50986fa

Please sign in to comment.