SlideShare a Scribd company logo
1 of 29
Download to read offline
Java言語で学ぶ
デザインパターン入門
深澤研究室 M0 稲垣 有哉
第14章
Chain of Responsibilityパターン
1
1
2
3
2
Chain of Responsibilityパターンとは
INDEX
サンプルプログラム
まとめ
INDEX
3
Chain of Responsibilityパターンとは
4
Chain of Responsibilityパターンとは
Chain of Responsibilityパターン
とは
「営業窓口」へ
行ってください
「ユーザーサポート部」
へ行ってください
「文書センター」
へ行ってください
5
Chain of Responsibilityパターンとは
たらい回しが必要な時とは?
複数のオブジェクトを鎖のようにつないでおき、そのオブジェクト
の鎖を順次渡り歩いて目的のオブジェクトを決定
Chain of Responsibilityパターン
とは
要求が発生した時にその要求を処理する
オブジェクトをダイレクトに決めれない場合
Chain of Responsibilityパターン
6
サンプルプログラム
名前 説明
Trouble 発生したトラブルを表すクラス
Support トラブルを解決する抽象クラス
NoSupport トラブルを解決する具象クラス①
LimitSupport トラブルを解決する具象クラス②
OddSupport トラブルを解決する具象クラス③
SpecialSupport トラブルを解決する具象クラス④
Main Supportの連鎖を作りトラブル
を起こす動作テスト用クラス
クラス一覧
7
public class Trouble {
private int number;
public Trouble(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public String toString() {
return "[Trouble " + number + "]";
}
}
Troubleクラス
発生したトラブルを表す
トラブル番号
トラブル番号を
[Trouble1]のように返す
8
public abstract class Support {
private String name;
private Support next;
public Support(String name) {
this.name = name;
}
public Support setNext(Support next) {
this.next = next;
return next;
}
public void support(Trouble trouble) {
if (resolve(trouble)) {
done(trouble);
} else if (next != null) {
next.support(trouble);
} else {
fail(trouble);
}
}
public String toString() {
return "[" + name + "]";
}
protected abstract boolean resolve(Trouble trouble);
protected void done(Trouble trouble) {
System.out.println(trouble + " is resolved by " + this + ".");
}
protected void fail(Trouble trouble) {
System.out.println(trouble + " cannot be resolved.");
}
}
Supportクラス
トラブルを解決する連鎖
を作るための抽象クラス
9
たらい回し先設定
たらい回し先
トラブル解決者
トラブル解決手順
解決用メソッド
解決時に表示する文字列
未解決時に表示する文字列
public class NoSupport extends Support {
public NoSupport(String name) {
super(name);
}
protected boolean resolve(Trouble trouble){
return false
}
}
NoSupportクラス
トラブル解決①
解決者名登録
全てのトラブルを
解決しない
10
public class LimitSupport extends Support {
private int limit;
public LimitSupport(String name, int limit)
super(name);
this.limit = limit;
}
protected boolean resolve(Trouble trouble){
if (trouble.getNumber() < limit) {
return true;
} else {
return false;
}
}
}
LimitSupportクラス
解決者名登録
11
トラブル解決②
リミット番号登録
リミット番号未満
であれば解決できる
public class OddSupport extends Support {
public OddSupport(String name) {
super(name);
}
protected boolean resolve(Trouble trouble) {
if (trouble.getNumber() % 2 == 1) {
return true;
} else {
return false;
}
}
}
OddSupportクラス
解決者名登録
12
トラブル解決③
奇数番号
であれば解決できる
public class SpecialSupport extends Support {
private int number;
public SpecialSupport(String name, int number) {
super(name);
this.number = number;
}
protected boolean resolve(Trouble trouble)
if (trouble.getNumber() == number) {
return true;
} else {
return false;
}
}
}
SpecialSupportクラス
解決者名登録
13
トラブル解決④
スペシャル番号と同じ
であれば解決できる
スペシャル番号登録
public class Main {
public static void main(String[] args) {
Support alice = new NoSupport("Alice");
Support bob = new LimitSupport("Bob", 100);
Support charlie = new SpecialSupport("Charlie", 429);
Support diana = new LimitSupport("Diana", 200);
Support elmo = new OddSupport("Elmo");
Support fred = new LimitSupport("Fred", 300);
alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred);
for (int i = 0; i < 500; i += 33) {
alice.support(new Trouble(i));
}
}
}
Mainクラス
14
解決者の登録
様々なトラブル発生
連鎖の形成
実行結果
15
名前 解決条件 たらい回し先
Alice なし Bob
Bob 100未満 Charlie
Charlie 429のみ Diana
Diana 200未満 Elmo
Elmo 奇数番号 Fred
Fred 300未満
トラブル363の例
実行結果
[Trouble 0] is resolved by [Bob].
[Trouble 33] is resolved by [Bob].
[Trouble 66] is resolved by [Bob].
[Trouble 99] is resolved by [Bob].
[Trouble 132] is resolved by [Diana].
[Trouble 165] is resolved by [Diana].
[Trouble 198] is resolved by [Diana].
[Trouble 231] is resolved by [Elmo].
[Trouble 264] is resolved by [Fred].
[Trouble 297] is resolved by [Elmo].
[Trouble 330] cannot be resolved.
[Trouble 363] is resolved by [Elmo].
[Trouble 396] cannot be resolved.
[Trouble 429] is resolved by [Charlie].
[Trouble 462] cannot be resolved.
[Trouble 495] is resolved by [Elmo].
16
名前 解決条件 たらい回し先
Alice なし Bob
Bob 100未満 Charlie
Charlie 429のみ Diana
Diana 200未満 Elmo
Elmo 奇数番号 Fred
Fred 300未満
17
まとめ
Chain of Responsibilityパターンの登場人物
Handler(処理者)の役
Supportクラス
Client(要求者)の役
Mainクラス →最初のConcreteHandler役に要求を出す役
→要求を処理するインタフェースを定める役
18
ConcreteHandler(具体的な処理者)の役
→要求を処理する具体的な役
NoSupportクラス LimitSupportクラス OddSupportクラス SpecialSupportクラス
Chain of Responsibilityパターンのポイント
要求を出す人と要求を処理する人を緩やかに結びつける
19
Chain of Responsibilityパターンのポイント
動的に連鎖の形態を変える
20
自分の仕事に集中できる
・状況の変化に応じてConcreteHandler役を組み替えれる
例)ウィンドウシステム(ユーザーが自由にボタンやテキストを追加するような場合)
・個々のConcreteHandler役で書くべき処理は固有の内容のみで良い
Chain of Responsibilityパターンのポイント
たらい回しで処理が遅くなるときもある!?
21
・要求と処理者の関係が固定的
・トレードオフの問題である。
・処理速度が非常に重要
使わない方が有効な場合あり
関連しているパターン
・Compositeパターン(11章)
・Commandパターン(22章)
→Handler役にはCompositeパターンが登場することが多い
→Handler役に対して投げられる「要求」にはCommandパターンが
使われることが多い
22
参考文献
1) 結城浩 増補改訂版Java言語で学ぶデザインパターン入門
SBクリエイティブ2004
23
B4-14 ChainOfResponsibility
Main Support
- name
- next
+support
+setNext
#resolve
NoSupport
#resolve
Request
LimitSupport
- limit
#resolve
OddSupport
#resolve
SpecialSupport
- number
#resolve
Client Handler
next
request
Request
ConcreteHandler1
request
ConcreteHandler2
request
Main alice bob charlie diana elmo fred
support
support
support
support
support
社員A 社員B 社員C 社員D
社長
社員C社員D
社員A 社員B社長

More Related Content

Recently uploaded

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
koheioishi1
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
YukiTerazawa
 

Recently uploaded (8)

2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

B4-14 ChainOfResponsibility