When you need some help in a hotel, you may ask the attendant
firstly, if the attend can’t help you, then the attendant may pass you
question to the assistant manager, if the attendant still can’t help
you, then the question maybe pass to the lobby manager.
The picture above shows the way your question passes through. In
fact, you don’t need to care the path your question pass through; you
just need to know, some one in the chain will give you an answer. And
it has a corresponding pattern called Chain of Responsibility.
The intent of this pattern is as below.
Avoid coupling the sender of a request to its receiver by
giving more than one object a chance to handle the request. Chain the
receiving objects and pass the request along the chain until an object
handles it.
– By THE GOF BOOK
In the scene above, we may need three classes to handle the help
message; further more, we need a common interface for these classes.
The class diagram will be as follows.
And if you want to use these classes, you need to specify each successor just as below.
- attendant
=
new
Attendant
()
;
- assistantManager
=
new
AssistantManager
()
;
- lobbyManager
=
new
LobbyManager
()
;
- attendant
.
setSuccessor
(
assistantManager
)
;
- assistantManager
.
setSuccessor
(
lobbyManager
)
;
When a client ask a question, you can simply call the
attendant.helpMsg(), and in the helpMsg() method of each concrete
helper, it can decide help or not depends on it’s own state. Eh, here I
simply use the random number to decide pass the request or handle it.
If you want to change the sequence, just change the successor by
calling the setSuccessor() method.
The above introduction is a use of this pattern; someone said
that it’s pure CoR (Chain of Responsibility). There is another use of
this pattern, eh, it’s not pure CoR. In the not pure way, all the
classes in the chain will work together to finish a job. Such as the
pipeline, you can let each class take one step, then pass the request
from the first one till the last one, then all the steps will finish,
so the job is done. That’s all for this pattern.
Download Full Project
Enjoy!
分享到:
相关推荐
Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss
"Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
Reusable Approaches for Object-Oriented... Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply functional design patterns such as Monad and more
https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码
《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。
Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...
《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...
Design Patterns - Elements of Reusable Object-Oriented Software [English] Design Patterns 英文版 带书签 解压密码:123456
Design Patterns-Elements of Reusable Object-Oriented Software 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源...
Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3
《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...