{"id":1093,"date":"2023-03-16T16:03:22","date_gmt":"2023-03-16T15:03:22","guid":{"rendered":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/?p=1093"},"modified":"2023-03-20T23:48:18","modified_gmt":"2023-03-20T22:48:18","slug":"wzorzec-projektowy-lancuch-odpowiedzialnosci","status":"publish","type":"post","link":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/2023\/03\/16\/wzorzec-projektowy-lancuch-odpowiedzialnosci\/","title":{"rendered":""},"content":{"rendered":"\n<h2 class=\"has-text-align-center has-black-color has-text-color wp-block-heading\" style=\"font-size:32px\">Wzorzec projektowy &#8222;\u0141a\u0144cuch odpowiedzialno\u015bci&#8221;<\/h2>\n\n\n\n<p class=\"has-black-color has-text-color\">Wzorzec projektowy \u0142a\u0144cuch odpowiedzialno\u015bci, jest wzorcem behawioralnym, kt\u00f3ry umo\u017cliwia przekazywanie zapyta\u0144 mi\u0119dzy obiektami, a\u017c do momentu, gdy kt\u00f3re\u015b z nich zdecyduje, \u017ce jest w stanie obs\u0142u\u017cy\u0107 dane zapytanie. Wzorzec ten pozwala na elastyczne i dynamiczne przypisanie obs\u0142ugi \u017c\u0105dania w trakcie dzia\u0142ania programu.<\/p>\n\n\n<div class=\"wp-block-image state-image box transparent\">\n<figure class=\"alignright size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/03\/favpng_industry-chain-rope-sticker.png\" alt=\"\" class=\"wp-image-1109\" width=\"400\" height=\"193\" srcset=\"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/03\/favpng_industry-chain-rope-sticker.png 800w, https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/03\/favpng_industry-chain-rope-sticker-300x144.png 300w, https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/03\/favpng_industry-chain-rope-sticker-768x370.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"has-black-color has-text-color\">Wzorzec \u0142a\u0144cuch odpowiedzialno\u015bci opiera si\u0119 na tworzeniu \u0142a\u0144cucha obiekt\u00f3w, z kt\u00f3rych ka\u017cdy mo\u017ce obs\u0142u\u017cy\u0107 dan\u0105 pro\u015bb\u0119 lub przekaza\u0107 j\u0105 dalej do nast\u0119pnego obiektu w \u0142a\u0144cuchu. <\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">Dzia\u0142a to na zasadzie hierarchii. W tym wzorcu klient przekazuje \u017c\u0105danie do pierwszego obiektu <br>w \u0142a\u0144cuchu. Je\u015bli obiekt ten nie mo\u017ce obs\u0142u\u017cy\u0107 \u017c\u0105dania, przekazuje je do wy\u017cszego w hierarchii obiektu i tak dalej, a\u017c \u017c\u0105danie zostanie obs\u0142u\u017cone lub dojdziemy do ko\u0144ca \u0142a\u0144cucha.<\/p>\n\n\n\n<h2 class=\"has-text-align-center has-black-color has-text-color wp-block-heading\" style=\"font-size:32px\">Podstawowe elementy wzorca \u0141a\u0144cuch odpowiedzialno\u015bci<\/h2>\n\n\n\n<p class=\"has-black-color has-text-color\"><strong><strong>Handler &#8211; <\/strong><\/strong>klasa bazowa, kt\u00f3ra okre\u015bla podstawowe metody i w\u0142a\u015bciwo\u015bci, kt\u00f3re musz\u0105 zosta\u0107 zaimplementowane przez ka\u017cdy kolejny element \u0142a\u0144cucha<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\"><strong>BaseHandler<\/strong> (opcjonalnie)-  bazowy obiekt obs\u0142uguj\u0105cy, kt\u00f3ry zawiera kod przygotowawczy kt\u00f3ry b\u0119dzie wsp\u00f3lny dla ka\u017cdego elementu \u0142a\u0144cucha odpowiedzialno\u015bci.<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\"><strong>ConcreteHandler <\/strong> &#8211; jest to konkretna implementacja klasy Handler, kt\u00f3ra ma za zadanie obs\u0142u\u017cy\u0107 \u017c\u0105danie lub przekaza\u0107 je dalej do nast\u0119pnego elementu w \u0142a\u0144cuchu.<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\"><strong>Client<\/strong>  &#8211; jest to klasa lub obiekt, kt\u00f3ry przekazuje \u017c\u0105danie do pierwszego elementu w \u0142a\u0144cuchu.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"has-text-align-center has-black-color has-text-color wp-block-heading\" style=\"font-size:32px\">Przyk\u0142ad implementacji we wzorcu \u0141a\u0144cuch odpowiedzialno\u015bci<\/h2>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>public abstract class Handler {\n   protected Handler nextHandler;\n \n   public void setNextHandler(Handler handler) {\n      nextHandler = handler;\n   }\n \n   public abstract void handleRequest(Request request);\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>public class Request {\n   private String type;\n   private String data;\n \n   public Request(String type, String data) {\n      this.type = type;\n      this.data = data;\n   }\n \n   public String getType() {\n      return type;\n   }\n \n   public String getData() {\n      return data;\n   }\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted has-black-color has-text-color\"><strong>Pierwszy Handler<\/strong><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>public class ConcreteHandler1 extends Handler {\n   @Override\n   public void handleRequest(Request request) {\n      if (request.getType().equals(\"type1\")) {\n         System.out.println(\"Request handled by ConcreteHandler1\");\n      } else {\n         if (nextHandler != null) {\n            nextHandler.handleRequest(request);\n         }\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color\"><code>\n<strong>Drugi Handler<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>public class ConcreteHandler2 extends Handler {\n   @Override\n   public void handleRequest(Request request) {\n      if (request.getType().equals(\"type2\")) {\n         System.out.println(\"Request handled by ConcreteHandler2\");\n      } else {\n         if (nextHandler != null) {\n            nextHandler.handleRequest(request);\n         }\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color\"><code>\n<strong>Klasa Client<\/strong><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>public class Client {\n   public static void main(String&#91;] args) {\n      Handler handler1 = new ConcreteHandler1();\n      Handler handler2 = new ConcreteHandler2();\n \n      handler1.setNextHandler(handler2);\n \n      Request request1 = new Request(\"type1\", \"data1\");\n      Request request2 = new Request(\"type2\", \"data2\");\n      Request request3 = new Request(\"type3\", \"data3\");\n \n      handler1.handleRequest(request1);\n      handler1.handleRequest(request2);\n      handler1.handleRequest(request3);\n   }\n}<\/code><\/pre>\n\n\n\n<p class=\"has-black-color has-text-color\">W tym przyk\u0142adzie mamy trzy klasy: <strong>Handler<\/strong>, <strong>ConcreteHandler1<\/strong> i <strong>ConcreteHandler2<\/strong>. Klasa <strong>Handler<\/strong> jest klas\u0105 <strong>abstrakcyjn\u0105<\/strong>, kt\u00f3ra definiuje metod\u0119 handleRequest i pole nextHandler. <br>Klasy ConcreteHandler1 i ConcreteHandler2 dziedzicz\u0105 po klasie Handler i implementuj\u0105 metod\u0119 handleRequest. Klasa Client tworzy instancje obu klas ConcreteHandler1 i ConcreteHandler2, \u0142\u0105czy je ze sob\u0105 i przekazuje im trzy r\u00f3\u017cne \u017c\u0105dania.<\/p>\n\n\n\n<p class=\"has-black-color has-text-color\">Po uruchomieniu programu otrzymujemy nast\u0119puj\u0105ce wyniki:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-text-color has-background\" style=\"background-color:#4e5156\"><code>Request handled by ConcreteHandler1\nRequest handled by ConcreteHandler2<\/code><\/pre>\n\n\n\n<p class=\"has-black-color has-text-color\">Wynika to z faktu, \u017ce pierwsze \u017c\u0105danie jest typu &#8222;<strong>type1<\/strong>&#8222;, kt\u00f3ry jest obs\u0142ugiwany przez ConcreteHandler1, a drugie \u017c\u0105danie jest typu &#8222;<strong>type2<\/strong>&#8222;, kt\u00f3ry jest obs\u0142ugiwany przez ConcreteHandler2. Trzecie \u017c\u0105danie zosta\u0142o zignorowane, poniewa\u017c \u017caden z handler\u00f3w nie obs\u0142ugiwa\u0142 tego typu.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer wrapped-paragraph\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Wzorzec projektowy &#8222;\u0141a\u0144cuch odpowiedzialno\u015bci&#8221; Wzorzec projektowy \u0142a\u0144cuch odpowiedzialno\u015bci, jest wzorcem behawioralnym, kt\u00f3ry umo\u017cliwia przekazywanie zapyta\u0144 mi\u0119dzy obiektami, a\u017c do momentu, gdy kt\u00f3re\u015b z nich zdecyduje, \u017ce jest w stanie obs\u0142u\u017cy\u0107 dane zapytanie. Wzorzec ten pozwala na elastyczne i dynamiczne przypisanie obs\u0142ugi \u017c\u0105dania w trakcie dzia\u0142ania programu. Wzorzec \u0142a\u0144cuch odpowiedzialno\u015bci opiera si\u0119 na tworzeniu \u0142a\u0144cucha obiekt\u00f3w,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1093","post","type-post","status-publish","format-standard","hentry","category-artykuly"],"_links":{"self":[{"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/1093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/comments?post=1093"}],"version-history":[{"count":9,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/1093\/revisions"}],"predecessor-version":[{"id":1168,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/posts\/1093\/revisions\/1168"}],"wp:attachment":[{"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/media?parent=1093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/categories?post=1093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maciej-klosinski.profesjonalnyprogramista.pl\/index.php\/wp-json\/wp\/v2\/tags?post=1093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}