Responsibility Chain Model

Source: Internet
Author: User


What is the responsibility chain model?


So that multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the request sender and the receiver. Connect the object to a chain and pass the request along the chain until an object processes it.


The focus of the responsibility chain model is on the "chain", where a chain processes similar requests and determines who will process the request and return the corresponding results.


Generic class diagram

Handler: the abstract processor. One is to define the handleMessage method for processing a request, the only method that is open to the outside. Instead, define the orchestration method setNext of a chain and set the next handler; the third is to define the two methods that must be implemented by the specific Requestor: The getHandlerLevel and the specific processing task that can be processed by the user. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + hybrid + NDQt + hybrid + m52M + hybrid + 1zbO1xMHpu + hybrid + cda-vcd4kpha + hybrid + PHByZSBjbGFzcz0 = "brush: java; "> abstract class Handler {protected Handler successor; public void SetSuccessor (Handler succes Sor) {this. successor = successor;} public abstract void HandleRequest (int request);} class ConcreteHandler1: Handler {public override void HandleRequest (int request) {if (request> = 0 & request <10) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. handleRequest (request) ;}} class ConcreteHandler2: Handler {public override void HandleRequest (int request) {if (request >=10 & request <20) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. handleRequest (request) ;}} class ConcreteHandler3: Handler {public override void HandleRequest (int request) {if (request >=20 & request <30) {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. HandleRequest (request );}}}

Knife Test


The following uses the data room toll system as an example to calculate the consumption amount of students offline to achieve the responsibility chain model.


We know that the amount needs to be calculated based on the time when the students go offline. The time can be divided into three types: preparation time, at least machine time, and at least machine time. Then we can divide these three time periods into three responsibilities for operations.


The Code is as follows:

Public Class Student 'student consumption time Private consumeTime As Single 'Student consumption amount Private consumeMoney As Single 'student consumption time Property getConsumeTime As Single Get Return consumeTime End Get Set (value As Single) consumeTime = value End Set End Property 'Property of student consumption amount getConsumeMoney As Single Get Return consumeMoney End Get Set (value As Single) consumeMoney = value End Set End PropertyEnd ClassPublic MustInherit Class Level 'defines the Level variable Protected superior As Level' to Set and manage the upper-Level Public Sub SetSuperior (superior As Level) Me. superior = superior End Sub 'abstract Class, used to process MustOverride Function RequestApplications (request As Student) As SingleEnd class' preparation time responsibilities Public Class PrepareTime Inherits Level Public Overrides Function RequestApplications (request As Student) as Single If request. getConsumeTime <5 then' request when the time is less than 5 minutes. getConsumeMoney = 0' here, ElseIf Not IsNothing (superior) then' is processed directly. If it takes more than 5 minutes, I will Not be authorized. Let the superior handle the superior. requestApplications (request) End If Return request. getConsumeMoney End FunctionEnd Class 'must at least perform the role of Public Class AtleastTime Inherits Level Public Overrides Function RequestApplications (request As Student) As Single If request. if getConsumeTime <30 then' is less than 30 minutes, I directly process the request. getConsumeMoney = 2 ElseIf Not IsNothing (superior) then' doesn't work, let the superior handle the superior. requestApplications (request) End If Return request. getConsumeMoney End FunctionEnd Class 'machine time responsibilities Public Class OnlineTime Inherits Level Public Overrides Function RequestApplications (request As Student) As Single If request. getConsumeTime> 30 then' I am the highest-Permission person and can process all requests. getConsumeMoney = 4/60 * request. getConsumeTime End If Return request. getConsumeMoney End FunctionEnd Class

The above is to calculate the student amount by matching the three roles in three paragraphs, so as to avoid excessive if ...... Else Branch judgment


Summary:


The advantage of the responsibility chain mode is to separate requests from processing. The request does not need to know who handled the request. The processor does not need to know the full picture of the request. Decoupling the two can improve the system flexibility.


Note: The number of nodes in the chain needs to be controlled to avoid extra-long links.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.