<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Md. Atiqul Islam</title>
    <description>The latest articles on DEV Community by Md. Atiqul Islam (@atiq1589).</description>
    <link>https://dev.to/atiq1589</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F878659%2F12e6e4be-0692-479d-8147-da635df2945f.jpeg</url>
      <title>DEV Community: Md. Atiqul Islam</title>
      <link>https://dev.to/atiq1589</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atiq1589"/>
    <language>en</language>
    <item>
      <title>Unleashing Flexibility in Your Code with the Chain of Responsibility Pattern</title>
      <dc:creator>Md. Atiqul Islam</dc:creator>
      <pubDate>Mon, 19 Aug 2024 17:02:01 +0000</pubDate>
      <link>https://dev.to/atiq1589/unleashing-flexibility-in-your-code-with-the-chain-of-responsibility-pattern-22ee</link>
      <guid>https://dev.to/atiq1589/unleashing-flexibility-in-your-code-with-the-chain-of-responsibility-pattern-22ee</guid>
      <description>&lt;p&gt;In software development, design patterns are essential tools for crafting flexible, maintainable, and scalable applications. They provide proven solutions to common problems, allowing developers to avoid reinventing the wheel and focus on solving more complex challenges. Among these patterns, the &lt;strong&gt;Chain of Responsibility (CoR)&lt;/strong&gt; stands out as a powerful mechanism for handling requests that need to be processed by multiple objects, especially when the exact object to handle the request isn’t known beforehand.&lt;/p&gt;

&lt;p&gt;In this blog post, we’ll delve into the Chain of Responsibility pattern through a practical example inspired by a customer support system. We’ll also discuss the importance of design patterns in software development, and how they contribute to creating robust and adaptable systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Necessity of Design Patterns
&lt;/h3&gt;

&lt;p&gt;Before diving into the example, let’s address why design patterns are necessary in software development. As systems grow in complexity, maintaining a clear, consistent, and flexible codebase becomes increasingly challenging. Design patterns offer several key benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Patterns provide reusable solutions that can be applied across different projects, reducing development time and effort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability:&lt;/strong&gt; By following well-known patterns, code becomes more structured and easier to maintain. New developers can more easily understand and contribute to the project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Design patterns often promote modularity, allowing systems to scale more efficiently by adding or modifying components without affecting the entire codebase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standardization:&lt;/strong&gt; Patterns create a common language among developers. When everyone on a team understands and uses the same patterns, communication and collaboration improve.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let’s explore how the Chain of Responsibility pattern can be implemented in a customer support system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Chain of Responsibility Pattern
&lt;/h3&gt;

&lt;p&gt;The Chain of Responsibility is a behavioural design pattern that allows a request to be passed through a chain of handlers, each with the opportunity to process it or pass it along to the next handler in the chain. This pattern decouples the sender of a request from its receiver, promoting flexibility and making it easier to manage complex workflows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Customer Support System
&lt;/h3&gt;

&lt;p&gt;Imagine a customer support system where requests range from simple password resets to complex technical issues that require managerial attention. Instead of writing a single function to handle all types of requests, we can use the Chain of Responsibility pattern to cleanly separate these responsibilities.&lt;/p&gt;

&lt;p&gt;Here’s how we can implement it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;abc&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abstractmethod&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SupportHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    The base class for all support handlers in the chain.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SupportHandler | None&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SupportHandler&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SupportHandler&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
        Sets the next handler in the chain.
        :param handler: The next handler to pass the request to.
        :return: The handler that was passed in, to allow chaining of handlers.
        &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;

    &lt;span class="nd"&gt;@abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
        Processes the support request. If unable to handle it, passes it to the next handler in the chain.
        :param request: The support request to process.
        :raises: Exception if no handler can process the request.
        &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BasicSupportHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SupportHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Handles basic support requests, such as password resets.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;password reset&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Basic Support: Password has been reset.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request could not be handled: No handler found for this request.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TechnicalSupportHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SupportHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Handles technical support requests that require more expertise.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;technical issue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Technical Support: Issue has been resolved.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request could not be handled: No handler found for this request.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ManagerSupportHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SupportHandler&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Handles requests that need managerial attention, such as complaints.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;complaint&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Manager Support: Your complaint has been addressed.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;_next_handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request could not be handled: No handler found for this request.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="c1"&gt;# Setting up the chain of responsibility
&lt;/span&gt;&lt;span class="n"&gt;support&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BasicSupportHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;support&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TechnicalSupportHandler&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;set_next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ManagerSupportHandler&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# Testing the chain with different requests
&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;support&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;password reset&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Basic Support: Password has been reset.
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Exception caught: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;support&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;technical issue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Technical Support: Issue has been resolved.
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Exception caught: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;support&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;complaint&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Manager Support: Your complaint has been addressed.
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Exception caught: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;support&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;billing issue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Output: Exception: Request could not be handled: No handler found for this request.
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Exception caught: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Breaking Down the Example
&lt;/h3&gt;

&lt;p&gt;In this example, we have three main components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;SupportHandler (Abstract Base Class):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This is the base class that defines the structure for all support handlers. Each handler knows how to set the next handler in the chain and tries to handle the request or pass it along.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BasicSupportHandler, TechnicalSupportHandler, and ManagerSupportHandler (Concrete Handlers):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;These are specific handlers that know how to handle certain types of requests. If a request doesn’t match their capability, they pass it along the chain.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request Handling:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The request is passed to the first handler (&lt;code&gt;BasicSupportHandler&lt;/code&gt;). If it’s a "password reset" request, it gets handled there. If not, it’s passed to &lt;code&gt;TechnicalSupportHandler&lt;/code&gt;, and so on, until a handler can process it or an exception is raised if no handler can.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Flexibility of the Chain of Responsibility
&lt;/h3&gt;

&lt;p&gt;The Chain of Responsibility pattern offers several advantages in the context of a customer support system:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Decoupling:&lt;/strong&gt; The client code that sends requests is decoupled from the code that handles them. This allows for greater flexibility, as the handling logic can be changed or extended without affecting the client code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Responsibility Principle:&lt;/strong&gt; Each handler is responsible for processing a specific type of request. This makes the code easier to understand, maintain, and extend.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extensibility:&lt;/strong&gt; Adding a new handler to the chain is straightforward and doesn’t require modifying existing code. For example, if you need to add a &lt;code&gt;BillingSupportHandler&lt;/code&gt; for handling billing issues, you can simply insert it into the chain without disrupting the other handlers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Assignment:&lt;/strong&gt; Handlers can be arranged dynamically, allowing the system to be configured at runtime to handle different types of requests in different orders.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to Use the Chain of Responsibility Pattern
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Handlers for a Request:&lt;/strong&gt; When a request might be handled by multiple objects, and the specific handler isn’t known in advance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Assignment of Responsibilities:&lt;/strong&gt; When you want to dynamically assign which object handles a request, especially in scenarios where you might add new types of handlers over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplifying Complex Conditionals:&lt;/strong&gt; When you have complex conditional logic that can be broken down into separate, manageable parts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The Chain of Responsibility pattern is a versatile tool for creating flexible and maintainable code. It allows developers to create systems where responsibilities are clearly defined and easily extendable. In our example of a customer support system, the pattern made it possible to handle different types of requests in a clean and modular way, promoting both clarity and scalability.&lt;/p&gt;

&lt;p&gt;Design patterns like Chain of Responsibility aren’t just theoretical concepts; they are practical solutions to real-world problems. By understanding and applying these patterns, developers can write code that is not only functional but also elegant and easy to manage.&lt;/p&gt;

&lt;p&gt;Next time you encounter a situation where multiple objects might need to process a request, consider implementing the Chain of Responsibility pattern. It might be the key to simplifying your code and making it more adaptable to future changes.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why Binary-Based Transmission Outperforms Text-Based: A Developer's Guide</title>
      <dc:creator>Md. Atiqul Islam</dc:creator>
      <pubDate>Sat, 10 Aug 2024 07:32:39 +0000</pubDate>
      <link>https://dev.to/atiq1589/why-binary-based-transmission-outperforms-text-based-a-developers-guide-3mia</link>
      <guid>https://dev.to/atiq1589/why-binary-based-transmission-outperforms-text-based-a-developers-guide-3mia</guid>
      <description>&lt;p&gt;When transmitting data over the internet or between devices, the way we encode and send that data can significantly impact performance. Two common approaches are text-based transmission and binary-based transmission. While text-based formats like JSON are easy to work with, binary-based formats offer significant efficiency advantages. Let’s explore why that is, using simple language and examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Text-Based Transmission: JSON Example&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Text-based transmission typically involves sending data in formats like JSON, which is both human-readable and widely used for APIs. For example, let's say you want to send the message "Hello World" in JSON format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hello World"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This JSON representation includes additional characters like {, ", and :, which help structure the data but also add extra bytes. If you were to send this over the network:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Size:&lt;/strong&gt; Approximately 28 bytes (depending on spacing and formatting).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Binary-Based Transmission: JSON in Binary&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Binary-based transmission involves encoding the data into a binary format. This can be done using serialization formats like &lt;strong&gt;Protocol Buffers (&lt;a href="https://protobuf.dev/" rel="noopener noreferrer"&gt;Protobuf&lt;/a&gt;)&lt;/strong&gt;. Protobuf allows you to define the structure of your data and then automatically generate the binary encoding.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Protobuf Example&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s say you define a Protobuf message schema like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;syntax = "proto3";

message MyMessage {
  string message = 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This schema defines a message type with a single field called &lt;code&gt;message&lt;/code&gt;. The number &lt;code&gt;1&lt;/code&gt; is a tag that uniquely identifies this field in the binary format.&lt;/p&gt;

&lt;p&gt;Now, if we serialize the JSON data &lt;code&gt;{"message": "Hello World"}&lt;/code&gt; using this Protobuf schema, it will be converted into a compact binary format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Binary Representation:&lt;/strong&gt; Protobuf will encode the data using the tag and the value.

&lt;ul&gt;
&lt;li&gt;The binary data might look something like this (in hexadecimal): &lt;code&gt;0a0b48656c6c6f20576f726c64&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Here’s how it breaks down:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0a&lt;/code&gt; is the field tag (1) and type (string).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0b&lt;/code&gt; is the length of the string ("Hello World" is 11 bytes).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;48656c6c6f20576f726c64&lt;/code&gt; is the ASCII representation of "Hello World."&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Size:&lt;/strong&gt; This binary representation might reduce the size to approximately &lt;strong&gt;15 bytes&lt;/strong&gt; (depending on the specific data and schema).&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Binary-Based Transmission is More Efficient&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Overhead:&lt;/strong&gt; Binary formats remove the need for extra characters like braces, quotes, and colons. This reduction in overhead means smaller data sizes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient Encoding:&lt;/strong&gt; Binary formats use compact identifiers and optimized data types, which further reduce the size of the transmitted data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Faster Processing:&lt;/strong&gt; Since binary data is closer to the machine’s native language, it’s processed faster than text-based formats, which need to be parsed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A Side-by-Side Comparison:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here’s a simple visual comparison to illustrate the difference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Text-Based (JSON) Representation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;-------------------------------------+         +-------------------+&lt;/span&gt;
&lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"message"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello World"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;        &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="c1"&gt;-----&amp;gt;  | 28 bytes over wire |&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;-------------------------------------+         +-------------------+&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Binary-Based (Protobuf) Representation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;-------------------------------------+         +-------------------+&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;Binary&lt;/span&gt; &lt;span class="n"&gt;representation&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;JSON&lt;/span&gt;       &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="c1"&gt;-----&amp;gt;  | ~15 bytes over wire |&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="c1"&gt;-------------------------------------+         +-------------------+&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;When to Use Binary-Based Transmission&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Binary-based transmission shines in scenarios where efficiency and performance are critical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;High-Volume Data:&lt;/strong&gt; When transmitting large amounts of data, the size reduction from binary encoding can significantly save bandwidth and speed up transmission.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-Time Systems:&lt;/strong&gt; In systems requiring low latency, such as financial trading platforms or online gaming, the speed of binary processing is advantageous.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource-Constrained Environments:&lt;/strong&gt; In mobile applications or IoT devices with limited processing power and bandwidth, binary formats help optimize performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While text-based formats like JSON are user-friendly and widely adopted, they come with performance costs. Binary-based transmission offers a more efficient alternative, particularly for structured data, where reducing size and processing time is crucial. By understanding and utilizing tools like Protobuf, you can build faster, more efficient systems that make better use of network resources.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
