{"id":6530,"date":"2023-03-26T18:46:07","date_gmt":"2023-03-26T18:46:07","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/layers\/"},"modified":"2023-03-26T18:46:07","modified_gmt":"2023-03-26T18:46:07","slug":"layers","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/layers\/","title":{"rendered":"Layers"},"content":{"rendered":"<p>The layers pattern splits a task into horizontal layers. Each layer has a specific responsibility and provides a service to a higher layer.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6525\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Layers.png\" alt=\"Layers\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Layers.png 1233w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Layers-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Layers-1024x520.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Layers-768x390.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>The Layers Pattern is an architectural pattern that helps, according to the book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Pattern-Oriented_Software_Architecture\"><span id=\"productTitle\" class=\"a-size-extra-large\">Pattern-Oriented Software Architecture, Volume 1&#8243;,<\/span><\/a> to bring structure into the mud.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Also_Known_as\"><\/span>Also Known as<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>N-tier architecture pattern<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Context\"><\/span>Context<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Large systems requiring disassembly<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Problem\"><\/span>Problem<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>A system that performs operations at different levels<\/li>\n<li>Higher levels use lower levels<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Solution\"><\/span>Solution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Structure the system in layers<\/li>\n<li>Services of a higher layer are based on the services of the lower layers<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Structure\"><\/span>Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6526\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/layersStructure.png\" alt=\"layersStructure\" width=\"367\" height=\"286\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/layersStructure.png 367w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/layersStructure-300x234.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6527\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/LayerCRC.png\" alt=\"LayerCRC\" width=\"350\" height=\"212\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/LayerCRC.png 678w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/LayerCRC-300x181.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><code><strong>Client<\/strong><\/code><\/p>\n<ul>\n<li>Accesses the top level<\/li>\n<\/ul>\n<p><code><strong>Layer J<\/strong><\/code><\/p>\n<ul>\n<li style=\"text-align: left;\">Encapsulates the specific role and responsibility of layer J<\/li>\n<li>Offers its services with the help of layer J -1<\/li>\n<li>Can only access layer J &#8211; 1<\/li>\n<\/ul>\n<p>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 <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-facade-pattern\">Facade Pattern<\/a>. The Facade Pattern provides a simplified interface to a complex system.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Examples\"><\/span>Examples<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The Layers Pattern is heavily used since the beginning of software development. Consequentially, there are many use cases:<\/p>\n<h4>OSI Model and TCP\/IP Model<\/h4>\n<p><em>The <b>Open Systems Interconnection model<\/b> (<b>OSI model<\/b>) is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Conceptual_model\" title=\"Conceptual model\">conce<\/a>p<a href=\"https:\/\/en.wikipedia.org\/wiki\/Conceptual_model\" title=\"Conceptual model\">tual model<\/a> that &#8216;provides a common basis for the coordination of [ISO] standards development for the purpose of systems interconnection&#8217;.<sup id=\"cite_ref-ISO-7498-1_2-0\" class=\"reference\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/OSI_model#cite_note-ISO-7498-1-2\">[2]<\/a><\/sup> 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<\/em>. (<a href=\"https:\/\/en.wikipedia.org\/wiki\/OSI_model\">https:\/\/en.wikipedia.org\/wiki\/OSI_model<\/a>)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6528\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Application_Layer.png\" alt=\"Osi model 7 layers\" width=\"400\" height=\"417\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Application_Layer.png 579w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/Application_Layer-288x300.png 288w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p style=\"text-align: center;\">Chunte7, CC BY-SA 3.0 &lt;<a href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/3.0&gt;\">https:\/\/creativecommons.org\/licenses\/by-sa\/3.0&gt;<\/a>, via Wikimedia Commons<\/p>\n<p>&nbsp;<\/p>\n<p>You may also very often hear about the simplified TCP\/IP model: <em>The <b>Internet protocol suite,<\/b> commonly known as <b>TCP\/IP,<\/b> is a framework for organizing the set of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Communication_protocol\" title=\"Communication protocol\">communication protocols<\/a> used in the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet\" title=\"Internet\">Internet<\/a> and similar <a href=\"https:\/\/en.wikipedia.org\/wiki\/Computer_network\" title=\"Computer network\">computer networks<\/a> according to functional criteria. The foundational protocols in the suite are the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transmission_Control_Protocol\" title=\"Transmission Control Protocol\">Transmission Control Protocol<\/a> (TCP), the <a href=\"https:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\" title=\"User Datagram Protocol\">User Datagram Protocol<\/a> (UDP), and the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_Protocol\" title=\"Internet Protocol\">Internet Protocol<\/a> (IP).<\/em> (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Internet_protocol_suite\">https:\/\/en.wikipedia.org\/wiki\/Internet_protocol_suite<\/a>)<\/p>\n<\/p>\n<h4>Embedded Systems<\/h4>\n<p>When developing software for embedded systems, you typically use various layers of abstraction in C++.<\/p>\n<ul>\n<li>You typically start with the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Board_support_package\"> board support package<\/a> (BSP). The BSP contains board-specific configurations, such as boot firmware and device drivers, so the embedded operating system can work.<\/li>\n<li>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hardware_abstraction\">hardware abstraction layer <\/a>(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.<\/li>\n<\/ul>\n<h4>Extend\/Embed Python in C\/C++<\/h4>\n<p>Extending Python in C\/C++ consists of the following steps:<\/p>\n<ol>\n<li>Convert the values from Python to C\/C++.<\/li>\n<li>Use the converted values to execute the C\/C++ functionality.<\/li>\n<li>Convert the results from C\/C++ to Python.<\/li>\n<\/ol>\n<p>Embedding does the same reversely. What is happening on the Python and the C layer? Here is the simplified strategy.<\/p>\n<ul>\n<li>All Python types, such as <code>int<\/code>, inherit from <code>object<\/code>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6529\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object.png\" alt=\"object\" width=\"650\" height=\"104\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object.png 2460w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object-300x48.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object-1024x164.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object-768x123.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object-1536x246.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/03\/object-2048x328.png 2048w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<ul>\n<li>The C pendant to the data type object is the C struct <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html\"><code>PyObject<\/code><\/a>. C is not object-oriented. <code>PyObject<\/code> is a kind of the starting point of the Python object&#8217;s memory. You can study its implementation on GitHub: <code><a href=\"https:\/\/github.com\/python\/cpython\/blob\/d93605de7232da5e6a182fd1d5c220639e900159\/Objects\/object.c\">object.c<\/a><\/code>. <code>PyObject<\/code> essentially has a reference counter and a pointer to the corresponding type.<\/li>\n<\/ul>\n<p>When you call a method on a Python type, this call goes to the C struct <code>PyObject<\/code>, defined in <code>object.c<\/code>. Inside<code> object.c<\/code>, the C function <a href=\"https:\/\/docs.python.org\/3\/c-api\/structures.html#c.Py_TYPE\">Py_TYPE<\/a> 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 <code>PyObject<\/code> is called if possible.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Pros_and_Cons\"><\/span>Pros and Cons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Pros\"><\/span>Pros<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4>Replacement of Layers<\/h4>\n<p>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.<\/p>\n<h4>Testability<\/h4>\n<p>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.<\/p>\n<h4>Development<\/h4>\n<p>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.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Cons\"><\/span>Cons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4>Granularity of Layers<\/h4>\n<p>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.<\/p>\n<h4>Performance<\/h4>\n<p>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.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_Next\"><\/span>What&#8217;s Next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The layers pattern splits a task into horizontal layers. Each layer has a specific responsibility and provides a service to a higher layer.<\/p>\n","protected":false},"author":21,"featured_media":6525,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6530"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6530\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6525"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}