Use of Callbacks in Layered Architecture

Computer NetworkInternetMCA

Layered Architecture

A layered architecture splits a system into many groups, each of which contains code that addresses a specific issue area, and these groups are referred to as layers.

The majority of enterprise-level apps have a three-layer high-level application architecture.

  • The Presentation layer
  • The Business layer
  • The Persistence layer

What is a Callback?

A callback, sometimes known as a "call-after" function in computer programming, is any executable code that is supplied as an argument to other code, with the expectation that the other code will call back (execute) the input at a specific time. This execution can take place at once, as in a synchronous callback, or it can take place later, as in an asynchronous callback.

Callbacks are implemented in a variety of methods in programming languages, with subroutines, lambda expressions, blocks, and function pointers being the most common.

Use of Callbacks in Layered Architecture

Upper layers are developed to make things easier to use (like SDKs), whereas lower layers are the real layers that interface with the network (for a networking-based project) or system-level calls (for OS-based projects).

As a result, we may call a function defined (and declared) in the lower layer straight from a source file in the higher layer and provide data through function parameters. However, we cannot simply call an upper-layer function from lower-layer functions, since this would result in a circular dependence. As a result, Callbacks enter the picture.

  • Let's suppose upperlayer.c and lowerlayer.c are the upper layer and the lower layer source files, respectively. lowerlayer.h is the lowerlayer.c header file.

  • The function reference of notify_observer() is given to lowerlayer.c as an argument of register callback in upperlayer.c.

  • In the bottom layer, this is referred to as registering callbacks. The bottom layer is now aware of the notify_observer function reference.

  • The register callback() function simply saves the function reference in the global function pointer g_notify_ob, allowing any file function to call notify_observer().

  • When the lower layer needs to convey data to the higher layer, it just calls notify_observer() by the calling g_notify_ob().

Published on 28-Oct-2021 07:18:51