GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-design-patterns/layers at master · tmollard/java-design-patterns

Expand file tree

/

layers

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

layers

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
etc
etc
 
 
src
src
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

README.md

Outline
layout pattern
title Layers
folder layers
permalink /patterns/layers/
pumlformat svg
categories Architectural
tags
Decoupling

Intent

Layers is an architectural pattern where software responsibilities are divided among the different layers of the application.

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. Cake consist of layers and topping.

@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.

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.

public interface View {

  void render();

}

public class CakeViewImpl implements View {

  private static final Logger LOGGER = LoggerFactory.getLogger(CakeViewImpl.class);

  private final CakeBakingService cakeBakingService;

  public CakeViewImpl(CakeBakingService cakeBakingService) {
    this.cakeBakingService = cakeBakingService;
  }

  public void render() {
    cakeBakingService.getAllCakes().forEach(cake -> LOGGER.info(cake.toString()));
  }
}

Class diagram

alt text

Applicability

Use the Layers architecture when

  • You want clearly divide software responsibilities into different parts of the program.
  • You want to prevent a change from propagating throughout the application.
  • You want to make your application more maintainable and testable.

Credits

  • Pattern Oriented Software Architecture Volume 1: A System of Patterns
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.