{"id":2969,"date":"2024-01-05T16:00:13","date_gmt":"2024-01-05T16:00:13","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=2969"},"modified":"2025-07-07T10:57:22","modified_gmt":"2025-07-07T10:57:22","slug":"java-chain-of-responsibility","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/java-chain-of-responsibility\/","title":{"rendered":"Java design pattern Chain of Responsibility"},"content":{"rendered":"<p>Today we will look at the first <strong>Java<\/strong> <strong>design pattern<\/strong> from the category of <strong>behavioural patterns<\/strong> &#8211; Chain of Responsibility. Design patterns in this category deal with the <strong>interaction<\/strong> between objects and their <strong>responsibility<\/strong>.<\/p>\n<h2>What is the Chain of Responsibility design pattern?<\/h2>\n<p>The <strong>Chain of Responsibility<\/strong> design pattern is a pattern that allows multiple objects to process requests without the object needing to know who its successor in the chain is. Each object in the chain has the ability to process the request, but if it doesn&#8217;t know how to process it, it passes it on to the next object in the chain.<\/p>\n<h2>What problem does the Chain of Responsibility design pattern solve?<\/h2>\n<p>It solves the problem of processing requests in a chain, so that instead of the request being sent to a specific object, the request passes through a chain of objects, where each object in the chain can be responsible for processing the request, but if it doesn&#8217;t process it, it passes it on to the next object in the chain. The goal is to allow multiple objects to process a request without having to specify exactly which object will process it, thus reducing the dependency between the sender and receiver of the request.<\/p>\n<h2>Example of a Chain of Responsibility implementation in Java<\/h2>\n<p>We will now write a program that uses the <strong>Chain of Responsibility<\/strong> design pattern to simulate the approval of employee requests in a company. The aim of this pattern is to allow multiple objects to process a request without knowing exactly which object will process it. The objects are chained together and each object in the chain decides whether it can handle the request or pass it on to the next object in the chain.  <\/p>\n<p>There are three types of employee in this program &#8211; <strong>Employee<\/strong>, <strong>Supervisor<\/strong> and <strong>Director<\/strong>, each of which implements an abstract class, <strong>RequestApprover<\/strong>, which contains common functionality for all employees.<\/p>\n<p><strong><u>RequestApprover.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic abstract class SchvalovatelPoziadavky {\n    String meno;\n    SchvalovatelPoziadavky dalsiSchvalovatel;\n    private SchvalovatelPoziadavky(){\n\n    }\n    public SchvalovatelPoziadavky(String meno){\n        this.meno = meno;\n    }\n    public abstract void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel);\n\n    public void posuditPoziadavku(String poziadavka)\n    {\n        if(this.dalsiSchvalovatel != null)\n            this.dalsiSchvalovatel.posuditPoziadavku(poziadavka);\n        else\n            System.out.println(poziadavka + &quot; - poziadavka bola zamietnuta!&quot;);\n    }\n}<\/code><\/pre>\n<p>The <strong>RequestApprover<\/strong> abstract class defines common methods and contains a reference to the next approver in the chain.<\/p>\n<p><strong><u>Employee.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class Zamestnanec extends SchvalovatelPoziadavky {\n    public Zamestnanec() {\n        super(&quot;zamestnanec&quot;);\n    }\n    @Override\n    public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {\n        this.dalsiSchvalovatel = dalsiSchvalovatel;\n    }\n    @Override\n    public void posuditPoziadavku(String poziadavka) {\n        if(poziadavka == &quot;NAVSTEVA_LEKARA&quot;) {\n            System.out.println(poziadavka + &quot; - &quot; + meno + &quot; schvalil poziadavku&quot;);\n        }\n        else {\n            super.posuditPoziadavku(poziadavka);\n        }\n    }\n}<\/code><\/pre>\n<p><strong><u>Supervisor.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\n\npublic class Veduci extends SchvalovatelPoziadavky {\n    public Veduci() {\n        super(&quot;veduci&quot;);\n    }\n    @Override\n    public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {\n        this.dalsiSchvalovatel = dalsiSchvalovatel;\n    }\n    @Override\n    public void posuditPoziadavku(String poziadavka) {\n        if(poziadavka == &quot;DOVOLENKA&quot;) {\n            System.out.println(poziadavka + &quot; - &quot; + meno + &quot; schvalil poziadavku&quot;);\n        }\n        else {\n            super.posuditPoziadavku(poziadavka);\n        }\n    }\n}<\/code><\/pre>\n<p><strong><u>Director.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">package designpatterns;\npublic class Riaditel extends SchvalovatelPoziadavky {\n    public Riaditel() {\n        super(&quot;riaditel&quot;);\n    }\n    @Override\n    public void nastavitDalsiehoSchvalovatela(SchvalovatelPoziadavky dalsiSchvalovatel) {\n        this.dalsiSchvalovatel = dalsiSchvalovatel;\n    }\n    @Override\n    public void posuditPoziadavku(String poziadavka) {\n        if(poziadavka == &quot;ZVYSENIE_PLATU&quot;) {\n            System.out.println(poziadavka + &quot; - &quot; + meno + &quot; schvalil poziadavku&quot;);\n        }\n        else {\n            super.posuditPoziadavku(poziadavka);\n        }\n    }\n}<\/code><\/pre>\n<p><strong>Employee<\/strong>, <strong>Supervisor<\/strong>, <strong>Director<\/strong> are specific classes representing individual employees. Each of them can approve certain types of requests, but if they are not authorized for some of them, they will pass them on to the next approver in the chain.<\/p>\n<p><strong><u>Main.java<\/u><\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">import designpatterns.Riaditel;\nimport designpatterns.SchvalovatelPoziadavky;\nimport designpatterns.Veduci;\nimport designpatterns.Zamestnanec;\n\npublic class Main {\n    public static void main(String[] args) {\n        \/\/ Clanky retaze\n        SchvalovatelPoziadavky zamestnanec = new Zamestnanec();\n        SchvalovatelPoziadavky veduci = new Veduci();\n        SchvalovatelPoziadavky riaditel = new Riaditel();\n        \/\/ Vytvorenie retaze zodpovednosti\n        zamestnanec.nastavitDalsiehoSchvalovatela(veduci);\n        veduci.nastavitDalsiehoSchvalovatela(riaditel);\n        \/\/ Posudenie poziadaviek\n        zamestnanec.posuditPoziadavku(&quot;NAVSTEVA_LEKARA&quot;);\n        zamestnanec.posuditPoziadavku(&quot;DOVOLENKA&quot;);\n        zamestnanec.posuditPoziadavku(&quot;ZVYSENIE_PLATU&quot;);\n        zamestnanec.posuditPoziadavku(&quot;FIREMNE_AUTO&quot;);\n    }\n}<\/code><\/pre>\n<p>The <em>main <\/em> method is to set up a chain of responsibility, create a hierarchy of individual employees and then consider the various requests. Each person tries to approve the request. If they cannot approve it, they delegate it to the next approver in the chain.  <\/p>\n<figure id=\"attachment_2396\" aria-describedby=\"caption-attachment-2396\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-2395 size-large\" src=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vysledok-z-prikladu-1150-500-1024x445.webp\" alt=\"The main method is to establish a chain of responsibility, create a hierarchy of individual employees and then consider the various requirements.\" width=\"640\" height=\"278\" srcset=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vysledok-z-prikladu-1150-500-1024x445.webp 1024w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vysledok-z-prikladu-1150-500-300x130.webp 300w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vysledok-z-prikladu-1150-500-768x334.webp 768w, https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/vysledok-z-prikladu-1150-500.webp 1150w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-2396\" class=\"wp-caption-text\">The output of this example.<\/figcaption><\/figure>\n<h2>Conclusion<\/h2>\n<p>The <strong>Chain of Responsibility<\/strong> design pattern is used in software design when we want to achieve a flexible way of processing requests, rather than creating a rigid link between the sender and the processor of the request and passing it to a chain of objects for processing.<\/p>\n<p>We have prepared the files with the above example in the form of code that you can run directly in Java. Download the <a href=\"https:\/\/msgprogramator.sk\/wp-content\/uploads\/2024\/01\/ChainOfResponsibility.zip\">Java <strong>Chain of Responsibility<\/strong> code here<\/a>.<\/p>\n<p>If you&#8217;re a <a href=\"https:\/\/msg-life.sk\/en\/jobs\/java-programmer-senior\/\">Java developer<\/a> looking for work, check out our <a href=\"https:\/\/msg-life.sk\/en\/benefits\/\">employee benefits<\/a> and respond to our <a href=\"https:\/\/msg-life.sk\/en\/jobs\/\">job offers<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Chain of Responsibility design pattern and what is it for? Read our new article and download the sample code.<\/p>\n","protected":false},"author":14,"featured_media":2399,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-2969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=2969"}],"version-history":[{"count":4,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2969\/revisions"}],"predecessor-version":[{"id":5265,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/2969\/revisions\/5265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=2969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=2969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=2969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}