Design Patterns in ActionScript-Chain of Responsib

本文介绍了一种设计模式——责任链模式,通过实例说明了如何避免请求发送者与接收者的耦合,让多个对象都有机会处理请求。具体展示了在一个酒店场景中,如何通过不同级别的管理者传递并处理客户请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

Question

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.

clip_image003

And if you want to use these classes, you need to specify each successor just as below.

  1. attendant = new Attendant () ;
  2. assistantManager = new   AssistantManager () ;
  3. lobbyManager = new   LobbyManager () ;
  4. attendant . setSuccessor ( assistantManager ) ;
  5. 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.DownloadDownload Full Project

Enjoy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值