{"id":668,"date":"2023-11-21T06:15:22","date_gmt":"2023-06-10T20:31:19","guid":{"rendered":"https:\/\/www.silicloud.com\/ja\/blog\/index.php\/2023\/11\/30\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/"},"modified":"2025-08-01T01:30:35","modified_gmt":"2025-07-31T16:30:35","slug":"java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/","title":{"rendered":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3"},"content":{"rendered":"<p>\u8cac\u4efb\u9023\u9396\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u884c\u52d5\u306e\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u4e00\u3064\u3067\u3059\u3002<\/p>\n<h2>\u30c1\u30a7\u30a4\u30f3\u30fb\u30aa\u30d6\u30fb\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3<\/h2>\n<p>\u30c1\u30a7\u30a4\u30f3\u30aa\u30d6\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u8a2d\u8a08\u306b\u304a\u3044\u3066\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u8907\u6570\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30c1\u30a7\u30fc\u30f3\u306b\u6e21\u3055\u308c\u3001\u305d\u308c\u3089\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306b\u4f7f\u7528\u3055\u308c\u308b\u3002\u305d\u3057\u3066\u3001\u30c1\u30a7\u30fc\u30f3\u5185\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3059\u308b\u306e\u306b\u8ab0\u304c\u9069\u3057\u3066\u3044\u308b\u304b\u3001\u305d\u3057\u3066\u6b21\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u81ea\u8eab\u3067\u6c7a\u5b9a\u3059\u308b\u3002<\/p>\n<h3>JDK\u5185\u306eChain of Responsibility\u30d1\u30bf\u30fc\u30f3\u306e\u4f8b<\/h3>\n<p>JDK\u5185\u306e\u8cac\u4efb\u9023\u9396\u30d1\u30bf\u30fc\u30f3\u306e\u4f8b\u3092\u898b\u3066\u3001\u305d\u306e\u30d1\u30bf\u30fc\u30f3\u306e\u5b9f\u751f\u6d3b\u306e\u4f8b\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u79c1\u305f\u3061\u306f\u3001try-catch\u30d6\u30ed\u30c3\u30af\u306e\u30b3\u30fc\u30c9\u5185\u306b\u8907\u6570\u306ecatch\u30d6\u30ed\u30c3\u30af\u3092\u6301\u3064\u3053\u3068\u304c\u3067\u304d\u308b\u3053\u3068\u3092\u77e5\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u5404catch\u30d6\u30ed\u30c3\u30af\u304c\u7279\u5b9a\u306e\u4f8b\u5916\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306e\u51e6\u7406\u88c5\u7f6e\u306e\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001try\u30d6\u30ed\u30c3\u30af\u3067\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u3068\u3001\u6700\u521d\u306ecatch\u30d6\u30ed\u30c3\u30af\u306b\u9001\u3089\u308c\u3066\u51e6\u7406\u3055\u308c\u307e\u3059\u3002\u3082\u3057catch\u30d6\u30ed\u30c3\u30af\u304c\u305d\u308c\u3092\u51e6\u7406\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u8981\u6c42\u306f\u30c1\u30a7\u30fc\u30f3\u5185\u306e\u6b21\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3001\u3064\u307e\u308a\u6b21\u306ecatch\u30d6\u30ed\u30c3\u30af\u306b\u8ee2\u9001\u3055\u308c\u307e\u3059\u3002\u3082\u3057\u6700\u5f8c\u306ecatch\u30d6\u30ed\u30c3\u30af\u3067\u3082\u51e6\u7406\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u4f8b\u5916\u306f\u30c1\u30a7\u30fc\u30f3\u306e\u5916\u90e8\u306b\u3042\u308b\u547c\u3073\u51fa\u3057\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u6295\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n<h3>Chain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u4f8b<\/h3>\n<p>Chain of Responsibility\u30d1\u30bf\u30fc\u30f3\u306e\u512a\u308c\u305f\u4f8b\u306e1\u3064\u306f\u3001ATM\u306e\u73fe\u91d1\u652f\u6255\u3044\u6a5f\u3067\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u306f\u652f\u6255\u3044\u91d1\u984d\u3092\u5165\u529b\u3057\u3001\u6a5f\u68b0\u306f50\u30c9\u30eb\u300120\u30c9\u30eb\u300110\u30c9\u30eb\u306a\u3069\u306e\u901a\u8ca8\u5358\u4f4d\u306e\u7d19\u5e63\u3092\u652f\u6255\u3044\u307e\u3059\u3002\u30e6\u30fc\u30b6\u30fc\u304c10\u306e\u500d\u6570\u3067\u306a\u3044\u91d1\u984d\u3092\u5165\u529b\u3057\u305f\u5834\u5408\u3001\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002\u3053\u306e\u89e3\u6c7a\u7b56\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u3001Chain of Responsibility\u30d1\u30bf\u30fc\u30f3\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002\u30c1\u30a7\u30fc\u30f3\u306f\u3001\u4ee5\u4e0b\u306e\u753b\u50cf\u3068\u540c\u3058\u9806\u5e8f\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3057\u307e\u3059\u3002\u306a\u304a\u3001\u3053\u306e\u89e3\u6c7a\u7b56\u306f\u5358\u4e00\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u7c21\u5358\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u304c\u3001\u8907\u96d1\u3055\u304c\u5897\u3057\u3001\u89e3\u6c7a\u7b56\u304c\u5f37\u304f\u7d50\u5408\u3055\u308c\u308b\u3068\u3044\u3046\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u305f\u304c\u3063\u3066\u300150\u30c9\u30eb\u300120\u30c9\u30eb\u300110\u30c9\u30eb\u306e\u7d19\u5e63\u3092\u652f\u6255\u3046\u305f\u3081\u306e\u30c7\u30a3\u30b9\u30da\u30f3\u30b9\u30b7\u30b9\u30c6\u30e0\u306e\u30c1\u30a7\u30fc\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h3>Chain of Responsibility \u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 &#8211; \u57fa\u672c\u30af\u30e9\u30b9\u3068\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9<\/h3>\n<p>\u79c1\u305f\u3061\u306f\u3001\u91d1\u984d\u3092\u30c7\u30a3\u30b9\u30da\u30f3\u30b9\u306b\u4f7f\u308f\u308c\u308b\u901a\u8ca8\u3092\u4fdd\u7ba1\u3059\u308bCurrency\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002Currency.java\u306f\u30c1\u30a7\u30fc\u30f3\u306e\u5b9f\u88c5\u306b\u3088\u3063\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\npublic class Currency {\r\n\r\n\tprivate int amount;\r\n\t\r\n\tpublic Currency(int amt){\r\n\t\tthis.amount=amt;\r\n\t}\r\n\t\r\n\tpublic int getAmount(){\r\n\t\treturn this.amount;\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>\u57fa\u672c\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306b\u306f\u3001\u30c1\u30a7\u30fc\u30f3\u5185\u306e\u6b21\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u5b9a\u7fa9\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u304c\u5fc5\u8981\u3067\u3059\u3002\u79c1\u305f\u3061\u306eATM\u30c7\u30a3\u30b9\u30da\u30f3\u30b9\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002DispenseChain.java<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\npublic interface DispenseChain {\r\n\r\n\tvoid setNextChain(DispenseChain nextChain);\r\n\t\r\n\tvoid dispense(Currency cur);\r\n}\r\n<\/code><\/pre>\n<h3>Chain of Responsibilities\u30d1\u30bf\u30fc\u30f3 &#8211; \u30c1\u30a7\u30fc\u30f3\u5b9f\u88c5<\/h3>\n<p>\u79c1\u305f\u3061\u306f\u3001DispenseChain\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9f\u88c5\u3057\u3001dispense\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u3092\u63d0\u4f9b\u3059\u308b\u7570\u306a\u308b\u30d7\u30ed\u30bb\u30c3\u30b5\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u79c1\u305f\u3061\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u309250\u30c9\u30eb\u300120\u30c9\u30eb\u300110\u30c9\u30eb\u306e3\u7a2e\u985e\u306e\u901a\u8ca8\u7d19\u5e63\u3067\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u958b\u767a\u3057\u3066\u3044\u308b\u305f\u3081\u30013\u3064\u306e\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002Dollar50Dispenser.java\u3067\u3059\u3002<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\npublic class Dollar50Dispenser implements DispenseChain {\r\n\r\n\tprivate DispenseChain chain;\r\n\t\r\n\t@Override\r\n\tpublic void setNextChain(DispenseChain nextChain) {\r\n\t\tthis.chain=nextChain;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void dispense(Currency cur) {\r\n\t\tif(cur.getAmount() &gt;= 50){\r\n\t\t\tint num = cur.getAmount()\/50;\r\n\t\t\tint remainder = cur.getAmount() % 50;\r\n\t\t\tSystem.out.println(\"Dispensing \"+num+\" 50$ note\");\r\n\t\t\tif(remainder !=0) this.chain.dispense(new Currency(remainder));\r\n\t\t}else{\r\n\t\t\tthis.chain.dispense(cur);\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>20\u30c9\u30eb\u306e\u30c7\u30a3\u30b9\u30da\u30f3\u30b5\u30fc.java<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\npublic class Dollar20Dispenser implements DispenseChain{\r\n\r\n\tprivate DispenseChain chain;\r\n\t\r\n\t@Override\r\n\tpublic void setNextChain(DispenseChain nextChain) {\r\n\t\tthis.chain=nextChain;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void dispense(Currency cur) {\r\n\t\tif(cur.getAmount() &gt;= 20){\r\n\t\t\tint num = cur.getAmount()\/20;\r\n\t\t\tint remainder = cur.getAmount() % 20;\r\n\t\t\tSystem.out.println(\"Dispensing \"+num+\" 20$ note\");\r\n\t\t\tif(remainder !=0) this.chain.dispense(new Currency(remainder));\r\n\t\t}else{\r\n\t\t\tthis.chain.dispense(cur);\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>Dollar10Dispenser.java\u3092\u65e5\u672c\u8a9e\u3067\u8868\u73fe\u3059\u308b\u3068\u3001Option 1: \u5341\u30c9\u30eb\u30c7\u30a3\u30b9\u30da\u30f3\u30b5.java<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\npublic class Dollar10Dispenser implements DispenseChain {\r\n\r\n\tprivate DispenseChain chain;\r\n\t\r\n\t@Override\r\n\tpublic void setNextChain(DispenseChain nextChain) {\r\n\t\tthis.chain=nextChain;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void dispense(Currency cur) {\r\n\t\tif(cur.getAmount() &gt;= 10){\r\n\t\t\tint num = cur.getAmount()\/10;\r\n\t\t\tint remainder = cur.getAmount() % 10;\r\n\t\t\tSystem.out.println(\"Dispensing \"+num+\" 10$ note\");\r\n\t\t\tif(remainder !=0) this.chain.dispense(new Currency(remainder));\r\n\t\t}else{\r\n\t\t\tthis.chain.dispense(cur);\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>\u3053\u3053\u3067\u6ce8\u76ee\u3059\u3079\u304d\u91cd\u8981\u306a\u70b9\u306f\u3001dispense\u30e1\u30bd\u30c3\u30c9\u306e\u5b9f\u88c5\u3067\u3059\u3002\u3059\u3079\u3066\u306e\u5b9f\u88c5\u304c\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3057\u3001\u91d1\u984d\u306b\u57fa\u3065\u3044\u3066\u4e00\u90e8\u307e\u305f\u306f\u3059\u3079\u3066\u3092\u51e6\u7406\u3057\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u3053\u3068\u306b\u6c17\u4ed8\u304f\u3067\u3057\u3087\u3046\u3002\u3082\u3057\u4e00\u3064\u306e\u30c1\u30a7\u30fc\u30f3\u304c\u5b8c\u5168\u306b\u51e6\u7406\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u305d\u306e\u6b8b\u308a\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u51e6\u7406\u3059\u308b\u305f\u3081\u306b\u6b21\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u30fc\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308a\u307e\u3059\u3002\u3082\u3057\u30d7\u30ed\u30bb\u30c3\u30b5\u30fc\u304c\u4f55\u3082\u51e6\u7406\u3067\u304d\u306a\u3044\u5834\u5408\u3001\u540c\u3058\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6b21\u306e\u30c1\u30a7\u30fc\u30f3\u306b\u8ee2\u9001\u3057\u307e\u3059\u3002<\/p>\n<h3>\u8cac\u4efb\u9023\u9396\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 &#8211; \u30c1\u30a7\u30fc\u30f3\u306e\u4f5c\u6210<\/h3>\n<p>\u3053\u308c\u306f\u975e\u5e38\u306b\u91cd\u8981\u306a\u30b9\u30c6\u30c3\u30d7\u3067\u3042\u308a\u3001\u79c1\u305f\u3061\u306f\u6ce8\u610f\u6df1\u304f\u30c1\u30a7\u30fc\u30f3\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3055\u3082\u306a\u3044\u3068\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u306f\u4e00\u5207\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u53d6\u308c\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001\u79c1\u305f\u3061\u306e\u5b9f\u88c5\u3067\u306f\u3001\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u30c1\u30a7\u30fc\u30f3\u3092Dollar10Dispenser\u3001\u6b21\u306bDollar20Dispenser\u3068\u3057\u3066\u4fdd\u6301\u3059\u308b\u3068\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u306f\u4e8c\u756a\u76ee\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u306b\u8ee2\u9001\u3055\u308c\u305a\u3001\u30c1\u30a7\u30fc\u30f3\u306f\u7121\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u304c\u79c1\u305f\u3061\u306eATM\u30c7\u30a3\u30b9\u30da\u30f3\u30b5\u30fc\u306e\u5b9f\u88c5\u3001ATMDispenseChain.java\u3067\u3059\u3002<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.design.chainofresponsibility;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class ATMDispenseChain {\r\n\r\n\tprivate DispenseChain c1;\r\n\r\n\tpublic ATMDispenseChain() {\r\n\t\t\/\/ initialize the chain\r\n\t\tthis.c1 = new Dollar50Dispenser();\r\n\t\tDispenseChain c2 = new Dollar20Dispenser();\r\n\t\tDispenseChain c3 = new Dollar10Dispenser();\r\n\r\n\t\t\/\/ set the chain of responsibility\r\n\t\tc1.setNextChain(c2);\r\n\t\tc2.setNextChain(c3);\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tATMDispenseChain atmDispenser = new ATMDispenseChain();\r\n\t\twhile (true) {\r\n\t\t\tint amount = 0;\r\n\t\t\tSystem.out.println(\"Enter amount to dispense\");\r\n\t\t\tScanner input = new Scanner(System.in);\r\n\t\t\tamount = input.nextInt();\r\n\t\t\tif (amount % 10 != 0) {\r\n\t\t\t\tSystem.out.println(\"Amount should be in multiple of 10s.\");\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\t\/\/ process the request\r\n\t\t\tatmDispenser.c1.dispense(new Currency(amount));\r\n\t\t}\r\n\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u51fa\u529b\u304c\u5f97\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"post-pre\"><code>Enter amount to dispense\r\n530\r\nDispensing 10 50$ note\r\nDispensing 1 20$ note\r\nDispensing 1 10$ note\r\nEnter amount to dispense\r\n100\r\nDispensing 2 50$ note\r\nEnter amount to dispense\r\n120\r\nDispensing 2 50$ note\r\nDispensing 1 20$ note\r\nEnter amount to dispense\r\n15\r\nAmount should be in multiple of 10s.\r\n<\/code><\/pre>\n<h3>Chain of Responsibilities\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u30af\u30e9\u30b9\u56f3<\/h3>\n<p>\u79c1\u305f\u3061\u306eATM\u306e\u30c1\u30a7\u30fc\u30f3\u30aa\u30d6\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u5b9f\u88c5\u4f8b\u306f\u3001\u4ee5\u4e0b\u306e\u753b\u50cf\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>Chain of Responsibility \u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/h3>\n<ul class=\"post-ul\">\n<li>Client doesn\u2019t know which part of the chain will be processing the request and it will send the request to the first object in the chain. For example, in our program ATMDispenseChain is unaware of who is processing the request to dispense the entered amount.<\/li>\n<li>Each object in the chain will have it\u2019s own implementation to process the request, either full or partial or to send it to the next object in the chain.<\/li>\n<li>Every object in the chain should have reference to the next object in chain to forward the request to, its achieved by java composition.<\/li>\n<li>Creating the chain carefully is very important otherwise there might be a case that the request will never be forwarded to a particular processor or there are no objects in the chain who are able to handle the request. In my implementation, I have added the check for the user entered amount to make sure it gets processed fully by all the processors but we might not check it and throw exception if the request reaches the last object and there are no further objects in the chain to forward the request to. This is a design decision.<\/li>\n<li>Chain of Responsibility design pattern is good to achieve lose coupling but it comes with the trade-off of having a lot of implementation classes and maintenance problems if most of the code is common in all the implementations.<\/li>\n<\/ul>\n<h3>JDK\u5185\u306eChain of Responsibility\u30d1\u30bf\u30fc\u30f3\u306e\u4f8b<\/h3>\n<ul class=\"post-ul\">\n<li>java.util.logging.Logger#log()<\/li>\n<li>javax.servlet.Filter#doFilter()<\/li>\n<\/ul>\n<p>\u30c1\u30a7\u30fc\u30f3\u30aa\u30d6\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0a\u3067\u3059\u3002\u3053\u306e\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u304c\u304a\u6c17\u306b\u5165\u308a\u3044\u305f\u3060\u3051\u3001\u7406\u89e3\u304c\u6df1\u307e\u308b\u3053\u3068\u3092\u9858\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8cac\u4efb\u9023\u9396\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u884c\u52d5\u306e\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u306e\u4e00\u3064\u3067\u3059\u3002 \u30c1\u30a7\u30a4\u30f3\u30fb\u30aa\u30d6\u30fb\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 \u30c1\u30a7\u30a4\u30f3\u30aa\u30d6\u30ec\u30b9\u30dd\u30f3\u30b7\u30d3\u30ea\u30c6\u30a3\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u8a2d\u8a08\u306b\u304a\u3044\u3066\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u8907\u6570\u306e\u30aa [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[26,61],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-26","tag-61"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002\" \/>\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.silicloud.com\/ja\/blog\/java\u306b\u304a\u3051\u308bchain-of-responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\/\" \/>\n<meta property=\"og:locale\" content=\"ja_JP\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\" \/>\n<meta property=\"og:description\" content=\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/ja\/blog\/java\u306b\u304a\u3051\u308bchain-of-responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-10T20:31:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T16:30:35+00:00\" \/>\n<meta name=\"author\" content=\"\u967d, \u5411\u5b87\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u57f7\u7b46\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u967d, \u5411\u5b87\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593\" \/>\n\t<meta name=\"twitter:data2\" content=\"16\u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/\",\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/\",\"name\":\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#website\"},\"datePublished\":\"2023-06-10T20:31:19+00:00\",\"dateModified\":\"2025-07-31T16:30:35+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/4aef6196128551a0f5d66db46c42866c\"},\"description\":\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#breadcrumb\"},\"inLanguage\":\"ja\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/ja\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"ja\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/4aef6196128551a0f5d66db46c42866c\",\"name\":\"\u967d, \u5411\u5b87\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ad96b120e219751fa368cbd7d2c48ca01b59185e6ed394449bac72614a760bf3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ad96b120e219751fa368cbd7d2c48ca01b59185e6ed394449bac72614a760bf3?s=96&d=mm&r=g\",\"caption\":\"\u967d, \u5411\u5b87\"},\"url\":\"https:\/\/www.silicloud.com\/ja\/blog\/author\/hinatasora\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 - Blog - Silicon Cloud","description":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002","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.silicloud.com\/ja\/blog\/java\u306b\u304a\u3051\u308bchain-of-responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\/","og_locale":"ja_JP","og_type":"article","og_title":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3","og_description":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002","og_url":"https:\/\/www.silicloud.com\/ja\/blog\/java\u306b\u304a\u3051\u308bchain-of-responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-06-10T20:31:19+00:00","article_modified_time":"2025-07-31T16:30:35+00:00","author":"\u967d, \u5411\u5b87","twitter_card":"summary_large_image","twitter_misc":{"\u57f7\u7b46\u8005":"\u967d, \u5411\u5b87","\u63a8\u5b9a\u8aad\u307f\u53d6\u308a\u6642\u9593":"16\u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/","url":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/","name":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/#website"},"datePublished":"2023-06-10T20:31:19+00:00","dateModified":"2025-07-31T16:30:35+00:00","author":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/4aef6196128551a0f5d66db46c42866c"},"description":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3\u3092\u5206\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3002\u5b9f\u8df5\u7684\u306a\u4f8b\u3068\u30b3\u30fc\u30c9\u3001\u6ce8\u610f\u70b9\u3092\u542b\u3081\u3066\u521d\u5fc3\u8005\u306b\u3082\u7406\u89e3\u3067\u304d\u308b\u3088\u3046\u8aac\u660e\u3057\u307e\u3059\u3002","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#breadcrumb"},"inLanguage":"ja","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/ja\/blog\/"},{"@type":"ListItem","position":2,"name":"Java\u306b\u304a\u3051\u308bChain of Responsibility\u30c7\u30b6\u30a4\u30f3\u30d1\u30bf\u30fc\u30f3"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#website","url":"https:\/\/www.silicloud.com\/ja\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"ja"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/4aef6196128551a0f5d66db46c42866c","name":"\u967d, \u5411\u5b87","image":{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.silicloud.com\/ja\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ad96b120e219751fa368cbd7d2c48ca01b59185e6ed394449bac72614a760bf3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ad96b120e219751fa368cbd7d2c48ca01b59185e6ed394449bac72614a760bf3?s=96&d=mm&r=g","caption":"\u967d, \u5411\u5b87"},"url":"https:\/\/www.silicloud.com\/ja\/blog\/author\/hinatasora\/"},{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.silicloud.com\/ja\/blog\/java%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8bchain-of-responsibility%e3%83%87%e3%82%b6%e3%82%a4%e3%83%b3%e3%83%91%e3%82%bf%e3%83%bc%e3%83%b3\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":43508,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/posts\/668\/revisions\/43508"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/ja\/blog\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}