Layers

Layers

This post is a cross-post from www.ModernesCpp.com.

The layers pattern splits a task into horizontal layers. Each layer has a specific responsibility and provides a service to a higher layer.

The Layers Pattern is an architectural pattern that helps, according to the book "Pattern-Oriented Software Architecture, Volume 1", to bring structure into the mud.

Also Known as

  • N-tier architecture pattern

Context

  • Large systems requiring disassembly

Problem

  • A system that performs operations at different levels
  • Higher levels use lower levels

Solution

  • Structure the system in layers
  • Services of a higher layer are based on the services of the lower layers

Structure

No alt text provided for this image


No alt text provided for this image

 Client

  • Accesses the top level

Layer J

  • Encapsulates the specific role and responsibility of layer J
  • Offers its services with the help of layer J -1
  • Can only access layer J -1

Although not specified, most layered architectures consist of three or four layers. Each layer is independent of the other layer. In the pure version, a layer can only access its layer below. A layer can not access its upper layer because it would create additional dependencies and complicates the control structure. Additionally, another application cannot easily use a layer that depends on an upper layer. A layer often provides its functionality by implementing the Facade Pattern. The Facade Pattern provides a simplified interface to a complex system.

Examples

The Layers Pattern is heavily used since the beginning of software development. Consequentially, there are many use cases:

OSI Model and TCP/IP Model

The Open Systems Interconnection model (OSI model) is a conceptual model that 'provides a common basis for the coordination of [ISO] standards development for the purpose of systems interconnection'.[2] In the OSI reference model, the communications between a computing system are split into seven different abstraction layers: Physical, Data Link, Network, Transport, Session, Presentation, and Application. (https://en.wikipedia.org/wiki/OSI_model)

No alt text provided for this image
Chunte7, CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0>, via Wikimedia Commons

You may also very often hear about the simplified TCP/IP model: The Internet protocol suite, commonly known as TCP/IP, is a framework for organizing the set of communication protocols used in the Internet and similar computer networks according to functional criteria. The foundational protocols in the suite are the Transmission Control Protocol (TCP), the User Datagram Protocol (UDP), and the Internet Protocol (IP). (https://en.wikipedia.org/wiki/Internet_protocol_suite)

No alt text provided for this image

Modernes C++ Mentoring

Stay informed via E-Mail.

Embedded Systems

When developing software for embedded systems, you typically use various layers of abstraction in C++.

  • You typically start with the board support package (BSP). The BSP contains board-specific configurations, such as boot firmware and device drivers, so the embedded operating system can work.
  • The hardware abstraction layer (HAL) sits on top of the BSP. The HAL is an abstraction layer between the hardware and the software that runs on the embedded system. Its function is to hide differences in hardware from the operating system.

Extend/Embed Python in C/C++

Extending Python in C/C++ consists of the following steps:

  1. Convert the values from Python to C/C++.
  2. Use the converted values to execute the C/C++ functionality.
  3. Convert the results from C/C++ to Python.

Embedding does the same reversely. What is happening on the Python and the C layer? Here is the simplified strategy.

  • All Python types, such as int, inherit from object.

No alt text provided for this image

  • The C pendant to the data type object is the C struct PyObject. C is not object-oriented. PyObject is a kind of the starting point of the Python object's memory. You can study its implementation on GitHub: object.c. PyObject essentially has a reference counter and a pointer to the corresponding type.

When you call a method on a Python type, this call goes to the C struct PyObject, defined in object.c. Inside object.c, the C function Py_TYPE deduces the object type and calls the corresponding function on the C layer. This means if the corresponding method is implemented on the deduced type this one is called. If not, the default implementation on PyObject is called if possible.

Pros and Cons

Pros

Replacement of Layers

Each layer has a specific role and specific responsibilities. It offers its services for the higher layer through an interface. The higher layer depends only on the interface of its lower layer. Consequentially, the lower layer can be easily replaced.

Testability

Each layer encapsulated its services. This makes it straightforward to test the functionality of each subsystem. More fine-granular tests, such as unit tests, must be applied inside the layers.

Development

Each layer can be implemented in isolation thanks to the separation of concern of the various layers. First, the interface of the layers has to be defined.

Cons

Granularity of Layers

It may be challenging to find the appropriate granularity of layers. Too many layers may cause layers that have only minimal responsibility. Additionally, the architecture may be very difficult to understand. Too few layers make it complicated to replace, test, and develop them in isolation.

Performance

A client call triggers a sequence of calls ending in the lowest layer. This sequence of calls may impact the performance of the application. This holds, in particular, true if layers are remote.

What's Next?

The Pipes-and-Filters Pattern is handy when you have a system that processes data in several steps, and each step should be developed independently. Let me write about it in my next post.


Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Dominik Vošček, and Rob North.

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

My special thanks to Embarcadero, PVS-Studio, and Tipi.build.

Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)

German

Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

No alt text provided for this image

To view or add a comment, sign in

More articles by Rainer Grimm

  • Charity run for ALS

    Tomorrow, on the 28th, there will be a charity run for ALS in Rottenburg. The course is exactly 1 km long.

    2 Comments
  • Small Safety Improvements in the C++ 26 Core Language

    Safety is an important concern in C++26. Contracts are probably the most important feature for safety.

    1 Comment
  • My ALS Journey (30/n): Cippi at the CppCon

    This week was very exciting for Cippi. She visited CppCon in Aurora, near Denver.

    2 Comments
  • Contracts: Evaluation Semantic

    After briefly presenting the details of contracts in my last article, “Contracts: A Deep Dive“, I would like to take a…

  • My ALS Journey (29/n): I feel Good

    I often receive messages asking about my health and wishing me well. I am very happy to receive these messages and just…

    5 Comments
  • Contracts: A Deep Dive

    August 25, 2025/in C++26/by Rainer GrimmI already introduced contracts in the article “Contracts in C++26”. In this…

  • My ALS Journey (28/n): Bureaucracy – The German Disease

    Today I want to write about a sad topic. Bureaucracy in the German healthcare system is becoming increasingly absurd.

    2 Comments
  • Data-Parallel Types: Algorithms

    The data-parallel types library has four special algorithms for SIMD vectors. The four special algorithms are min, max,…

  • My ALS Journey (27/n): An Emergency Call

    Firstly, I would like to say that I am doing very well and have made a full recovery from my incident. However, I would…

    8 Comments
  • Data-Parallel Types: Reduction

    In this article, I will discuss reduction and mask reduction for data-parallel types. Reduction A reduction reduces the…

Others also viewed

Explore content categories