{"id":44716,"date":"2015-09-30T06:29:28","date_gmt":"2015-09-30T03:29:28","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44716"},"modified":"2023-12-07T11:15:53","modified_gmt":"2023-12-07T09:15:53","slug":"chain-of-responsibility-design-pattern-2","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html","title":{"rendered":"Chain of Responsibility Design Pattern Example"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">Java Design Patterns<\/a>.<\/em><\/p>\n<p>In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of them. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/java-design-patterns\/\">here<\/a>!<\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#chain_responsibility_pattern\">1. Chain of Responsibility Pattern<\/a><\/dt>\n<dt><a href=\"#what_is_chain_responsibility\">2. What is the Chain of Responsibility Pattern<\/a><\/dt>\n<dt><a href=\"#implementing_chain_responsibility\">3. Implementing Chain of Responsibility<\/a><\/dt>\n<dt><a href=\"#when_to_use_chain_responsibility\">4. When to use the Chain of Responsibility Pattern<\/a><\/dt>\n<dt><a href=\"#chain_responsibility_in_jdk\">5. Chain of Responsibility in JDK<\/a><\/dt>\n<dt><a href=\"#download_chain_responsibility_code\">6. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"chain_responsibility_pattern\"><\/a>1. Chain of Responsibility Pattern<\/h2>\n<p>The Chain of Responsibility pattern is a behavior pattern in which a group of objects is chained together in a sequence and a responsibility (a request) is provided in order to be handled by the group. If an object in the group can process the particular request, it does so and returns the corresponding response. Otherwise, it forwards the request to the subsequent object in the group.<\/p>\n<p>For a real life scenario, in order to understand this pattern, suppose you got a problem to solve. If you are able to handle it on your own, you will do so, otherwise you will tell your friend to solve it. If he\u2019ll able to solve he will do that, or he will also forward it to some other friend. The problem would be forwarded until it gets solved by one of your friends or all your friends have seen the problem, but no one is able to solve it, in which case the problem stays unresolved.<\/p>\n<p>Let&#8217;s address a real life scenario. Your company has got a contract to provide an analytical application to a health company. The application would tell the user about the particular health problem, its history, its treatment, medicines, interview of the person suffering from it etc, everything that is needed to know about it. For this, your company receives a huge amount of data. The data could be in any format, it could text files, doc files, excels, audio, images, videos, anything that you can think of would be there.<\/p>\n<p>Now, your job is to save this data in the company\u2019s database. Users will provide the data in any format and you should provide them a single interface to upload the data into the database. The user is not interested, not even aware, to know that how you are saving the different unstructured data?<\/p>\n<p>The problem here is that you need to develop different handlers to save the various formats of data. For example, a text file save handler does not know how to save an mp3 file.<\/p>\n<p>To solve this problem you can use the Chain of Responsibility design pattern. You can create different objects which process different formats of data and chain them together. When a request comes to a single object, it will check whether it can process and handle the specfic file format. If it can, it will process it; otherwise, it will forward it to the next object chained to it. This design pattern also decouples the user from the object that is serving the request; the user is not aware which object is actually serving its request.<\/p>\n<p>Before solving the problem, let\u2019s first know more about the Chain of Responsibility design pattern.<\/p>\n<h2><a name=\"what_is_chain_responsibility\"><\/a>2. What is the Chain of Responsibility Pattern<\/h2>\n<p>The intent of this pattern is to avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. We chain the receiving objects and pass the request along the chain until an object handles it.<\/p>\n<p>This pattern is all about connecting objects in a chain of notification; as a notification travels down the chain, it\u2019s handled by the first object that is set up to deal with the particular notification.<\/p>\n<p>When there is more than one objects that can handle or fulfill a client request, the pattern recommends giving each of these objects a chance to process the request in some sequential order. Applying the pattern in such a case, each of these potential handlers can be arranged in the form of a chain, with each object having a reference to the next object in the chain. The first object in the chain receives the request and decides either to handle the request or to pass it on to the next object in the chain. The request flows through all objects in the chain one after the other until the request is handled by one of the handlers in the chain or the request reaches the end of the chain without getting processed.<\/p>\n<p><figure id=\"attachment_6580\" aria-describedby=\"caption-attachment-6580\" style=\"width: 434px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/chain_of_responsibility_class_diagram.jpg\"><img decoding=\"async\" class=\"size-full wp-image-6580\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/chain_of_responsibility_class_diagram.jpg\" alt=\"Figure 1\" width=\"434\" height=\"204\" \/><\/a><figcaption id=\"caption-attachment-6580\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\">Handler<\/span><\/p>\n<ol>\n<li>Defines an interface for handling requests.<\/li>\n<li>(Optionally) Implements the successor link.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">ConcreteHandler<\/span><\/p>\n<ol>\n<li>Handles requests it is responsible for.<\/li>\n<li>Can access its successor.<\/li>\n<li>If the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor.<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\">Client<\/span><\/p>\n<ol>\n<li>Initiates the request to a ConcreteHandler object on the chain.<\/li>\n<\/ol>\n<p>When a client issues a request, the request propagates along the chain until a ConcreteHandler object takes responsibility for handling it.<\/p>\n<h2><a name=\"implementing_chain_responsibility\"><\/a>3. Implementing Chain of Responsibility<\/h2>\n<p>To implement the Chain of Responsibility in order to solve the above problem, we will create an interface, Handler.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.chainofresponsibility;\n\npublic interface Handler {\n\n\tpublic void setHandler(Handler handler);\n\tpublic void process(File file);\n\tpublic String getHandlerName();\n}\n\n<\/pre>\n<p>The above interface contains two main methods, the <code>setHandler<\/code> and the process methods. The <code>setHandler<\/code> is used to set the next handler in the chain, whereas; the process method is used to process the request, only if the handler can able process the request. Optionally, we have the <code>getHandlerName<\/code> method which is used to return the handler\u2019s name.<\/p>\n<p>The handlers are designed to process files which contain data. The concrete handler checks if it&#8217;s able to handle the file by checking the file type, otherwise forwards to the next handler in the chain.<\/p>\n<p>The <code>File<\/code> class looks like this.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class File {\n\n\tprivate final String fileName;\n\tprivate final String fileType;\n\tprivate final String filePath;\n\n\tpublic File(String fileName, String fileType, String filePath){\n\t\tthis.fileName = fileName;\n\t\tthis.fileType = fileType;\n\t\tthis.filePath = filePath;\n\t}\n\n\tpublic String getFileName() {\n\t\treturn fileName;\n\t}\n\n\tpublic String getFileType() {\n\t\treturn fileType;\n\t}\n\n\tpublic String getFilePath() {\n\t\treturn filePath;\n\t}\n\n}\n<\/pre>\n<p>The <code>File<\/code> class creates simple file objects which contain the file name, file type, and the file path. The file type would be used by the handler to check if the file can be handled by them or not. If a handler can, it will process and save it, or it will forward it to the next handler in the chain.<\/p>\n<p>Let\u2019s see some concrete handlers now.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class TextFileHandler implements Handler {\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic TextFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"text\")){\n\t\t\tSystem.out.println(\"Process and saving text file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n}\n<\/pre>\n<p>The <code>TextFileHandler<\/code> is used to handle text files. It implements the <code>Handler<\/code> interface and overrides its three methods. It holds a reference to the next handler in the chain. In the <code>process<\/code> method, it checks the file type if the file type is text, it processes it or it forwards it to the next handler.<\/p>\n<p>The other handlers are similar to the above handler.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class DocFileHandler implements Handler{\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic DocFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"doc\")){\n\t\t\tSystem.out.println(\"Process and saving doc file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n\n}\n\npackage com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class AudioFileHandler implements Handler {\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic AudioFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"audio\")){\n\t\t\tSystem.out.println(\"Process and saving audio file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n\n}\n\npackage com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class ExcelFileHandler implements Handler{\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic ExcelFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"excel\")){\n\t\t\tSystem.out.println(\"Process and saving excel file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n}\n\npackage com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class ImageFileHandler implements Handler {\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic ImageFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"image\")){\n\t\t\tSystem.out.println(\"Process and saving image file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n\n}\n\npackage com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class VideoFileHandler implements Handler {\n\n\tprivate Handler handler;\n\tprivate String handlerName;\n\n\tpublic VideoFileHandler(String handlerName){\n\t\tthis.handlerName = handlerName;\n\t}\n\n\t@Override\n\tpublic void setHandler(Handler handler) {\n\t\tthis.handler = handler;\n\t}\n\n\t@Override\n\tpublic void process(File file) {\n\n\t\tif(file.getFileType().equals(\"video\")){\n\t\t\tSystem.out.println(\"Process and saving video file... by \"+handlerName);\n\t\t}else if(handler!=null){\n\t\t\tSystem.out.println(handlerName+\" fowards request to \"+handler.getHandlerName());\n\t\t\thandler.process(file);\n\t\t}else{\n\t\t\tSystem.out.println(\"File not supported\");\n\t\t}\n\n\t}\n\n\t@Override\n\tpublic String getHandlerName() {\n\t\treturn handlerName;\n\t}\n}\n<\/pre>\n<p>[ulp id=&#8217;7eyRwVlMDyxg6Ptw&#8217;]<br \/>\n&nbsp;<br \/>\nNow, let\u2019s test the code above.<\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.patterns.chainofresponsibility;\n\npublic class TestChainofResponsibility {\n\n\tpublic static void main(String[] args) {\n\t\tFile file = null;\n\t\tHandler textHandler = new TextFileHandler(\"Text Handler\");\n\t\tHandler docHandler = new DocFileHandler(\"Doc Handler\");\n\t\tHandler excelHandler = new ExcelFileHandler(\"Excel Handler\");\n\t\tHandler audioHandler = new AudioFileHandler(\"Audio Handler\");\n\t\tHandler videoHandler = new VideoFileHandler(\"Video Handler\");\n\t\tHandler imageHandler = new ImageFileHandler(\"Image Handler\");\n\n\t\ttextHandler.setHandler(docHandler);\n\t\tdocHandler.setHandler(excelHandler);\n\t\texcelHandler.setHandler(audioHandler);\n\t\taudioHandler.setHandler(videoHandler);\n\t\tvideoHandler.setHandler(imageHandler);\n\n\t\tfile = new File(\"Abc.mp3\", \"audio\", \"C:\");\n\t\ttextHandler.process(file);\n\n\t\tSystem.out.println(\"---------------------------------\");\n\n\t\tfile = new File(\"Abc.jpg\", \"video\", \"C:\");\n\t\ttextHandler.process(file);\n\n\t\tSystem.out.println(\"---------------------------------\");\n\n\t\tfile = new File(\"Abc.doc\", \"doc\", \"C:\");\n\t\ttextHandler.process(file);\n\n\t\tSystem.out.println(\"---------------------------------\");\n\n\t\tfile = new File(\"Abc.bat\", \"bat\", \"C:\");\n\t\ttextHandler.process(file);\n\t}\n\n}\n<\/pre>\n<p>The above program will have the following output.<\/p>\n<pre class=\"brush:bash\">Text Handler fowards request to Doc Handler\nDoc Handler fowards request to Excel Handler\nExcel Handler fowards request to Audio Handler\nProcess and saving audio file... by Audio Handler\n---------------------------------\nText Handler fowards request to Doc Handler\nDoc Handler fowards request to Excel Handler\nExcel Handler fowards request to Audio Handler\nAudio Handler fowards request to Video Handler\nProcess and saving video file... by Video Handler\n---------------------------------\nText Handler fowards request to Doc Handler\nProcess and saving doc file... by Doc Handler\n---------------------------------\nText Handler fowards request to Doc Handler\nDoc Handler fowards request to Excel Handler\nExcel Handler fowards request to Audio Handler\nAudio Handler fowards request to Video Handler\nVideo Handler fowards request to Image Handler\nFile not supported\n<\/pre>\n<p>In the example above, first we created different handlers and chained them. The chain starts from the text handler, which is used to process text files, to the doc handler and so on, till the last handler, the image handler.<\/p>\n<p>Then we created different file objects and passed it to the text handler. If the file can be processed by the text handler it does that, otherwise it forwards the file to the next chained handler. You can see in the output how the requested file was forwarded by the chained objects until it reached the appropriate handler.<\/p>\n<p>Also, please note down, we have not created a handler to process a bat file. So, it passes through all the handlers and results in the output &#8211; &#8220;File not supported&#8221;.<\/p>\n<p>The client code is decoupled from the served object. It only sends the request, and the request gets served by any one of the handlers in the chain or does not get processed in case there is support for it.<\/p>\n<h2><a name=\"when_to_use_chain_responsibility\"><\/a>4. When to use the Chain of Responsibility Pattern<\/h2>\n<p>Use Chain of Responsibility when<\/p>\n<ol>\n<li>More than one objects may handle a request, and the handler isn&#8217;t known a priori. The handler should be ascertained automatically.<\/li>\n<li>You want to issue a request to one of several objects without specifying the receiver explicitly.<\/li>\n<li>The set of objects that can handle a request should be specified dynamically.<\/li>\n<\/ol>\n<h2><a name=\"chain_responsibility_in_jdk\"><\/a>5. Chain of Responsibility in JDK<\/h2>\n<p>The following are the usages of the Chain of Responsibility Pattern in Java.<\/p>\n<ol>\n<li><code>java.util.logging.Logger#log()<\/code><\/li>\n<li><code>javax.servlet.Filter#doFilter()<\/code><\/li>\n<\/ol>\n<h2><a name=\"download_chain_responsibility_code\"><\/a>6. Download the Source Code<\/h2>\n<p>This was a lesson on the Chain of Responsibility Pattern. You may download the source code here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/ChainofResponsibility-Project.zip\"><strong>ChainofResponsibility-Project<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how those are implemented and utilized in Java. You will understand the reasons why patterns are so important and learn when and how to apply each one of &hellip;<\/p>\n","protected":false},"author":967,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[145],"class_list":["post-44716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-design-patterns"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Chain of Responsibility Design Pattern Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Chain of Responsibility Design Pattern Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-30T03:29:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-07T09:15:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rohit Joshi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Joshi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html\"},\"author\":{\"name\":\"Rohit Joshi\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\"},\"headline\":\"Chain of Responsibility Design Pattern Example\",\"datePublished\":\"2015-09-30T03:29:28+00:00\",\"dateModified\":\"2023-12-07T09:15:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html\"},\"wordCount\":1404,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Design Patterns\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html\",\"name\":\"Chain of Responsibility Design Pattern Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-30T03:29:28+00:00\",\"dateModified\":\"2023-12-07T09:15:53+00:00\",\"description\":\"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/chain-of-responsibility-design-pattern-2.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Chain of Responsibility Design Pattern Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/ddd1b95af381c5042c09833e6a108e20\",\"name\":\"Rohit Joshi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g\",\"caption\":\"Rohit Joshi\"},\"description\":\"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rohit-joshi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Chain of Responsibility Design Pattern Example - Java Code Geeks","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html","og_locale":"en_US","og_type":"article","og_title":"Chain of Responsibility Design Pattern Example - Java Code Geeks","og_description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-30T03:29:28+00:00","article_modified_time":"2023-12-07T09:15:53+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Rohit Joshi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rohit Joshi","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html"},"author":{"name":"Rohit Joshi","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20"},"headline":"Chain of Responsibility Design Pattern Example","datePublished":"2015-09-30T03:29:28+00:00","dateModified":"2023-12-07T09:15:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html"},"wordCount":1404,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Design Patterns"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html","name":"Chain of Responsibility Design Pattern Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-30T03:29:28+00:00","dateModified":"2023-12-07T09:15:53+00:00","description":"This article is part of our Academy Course titled Java Design Patterns. In this course you will delve into a vast number of Design Patterns and see how","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/chain-of-responsibility-design-pattern-2.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Chain of Responsibility Design Pattern Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/ddd1b95af381c5042c09833e6a108e20","name":"Rohit Joshi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/802a913bbb576309b246c8e839eaec9910390a9e7b8bf684a9706f8bfa3f6012?s=96&d=mm&r=g","caption":"Rohit Joshi"},"description":"Rohit Joshi is a Senior Software Engineer from India. He is a Sun Certified Java Programmer and has worked on projects related to different domains. His expertise is in Core Java and J2EE technologies, but he also has good experience with front-end technologies like Javascript, JQuery, HTML5, and JQWidgets.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/rohit-joshi"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/967"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44716"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44716\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}