Analysis of Android source code design patterns and practices (9)

Source: Internet
Author: User
Tags class manager

Analysis of Android source code design patterns and practices (9)
Chapter 9. Responsibility Chain Model 1. Definition

The responsibility chain mode is one of the behavioral design patterns. It gives multiple objects the opportunity to process requests, thus avoiding the coupling relationship between request senders and recipients. Connect these objects into a chain and pass the request along the chain until an object processes it.

2. Use Cases

1. Multiple objects can process the same request, but the specific object processing is dynamically determined at runtime.
2. submit a request to one of multiple objects if the request handler is ambiguous.
3. A group of objects must be specified dynamically to process requests.

3. Simple implementation

I think this example is very relevant. We have to reimburse the expenses for various reasons in the company. First, we need to contact our superior leadership for approval. If the reimbursement quota is within the authority of the leadership, it will be approved, otherwise, the leader is looking for his superiors for approval, and so on.

Abstract leadership

Public abstract class Leader {/*** superior Leader handler */protected Leader nextHandler; /*** process the report request ** @ param money: The amount of the report that can be approved **/public final void handleRequest (int money) {System. out. println (getLeader (); if (money <= limit () {handle (money);} else {System. out. println ("insufficient reimbursement quota, submit to the lead"); if (null! = NextHandler) {nextHandler. handleRequest (money) ;}}/ *** self-approved quota permission ** @ return quota */public abstract int limit (); /*** handle accounting reporting behavior ** @ param money specific amount */public abstract void handle (int money ); /*** get the handler ** @ return handler */public abstract String getLeader ();}

Team Lead (quota 1000 ):

Public class GroupLeader extends Leader {@ Override public int limit () {return 1000 ;}@ Override public void handle (int money) {System. out. println ("team lead approval reimbursement" + money + "Yuan") ;}@ Override public String getLeader () {return "current team lead ";}}

Supervisor (quota 5000 ):

Public class Director extends Leader {@ Override public int limit () {return 5000 ;}@ Override public void handle (int money) {System. out. println ("approved by the supervisor" + money + "Yuan") ;}@ Override public String getLeader () {return "Current supervisor ";}}

Manager (quota 10000 ):

Public class Manager extends Leader {@ Override public int limit () {return 10000 ;}@ Override public void handle (int money) {System. out. println ("Manager approves reimbursement" + money + "Yuan") ;}@ Override public String getLeader () {return "current Manager ";}}

Boss (quota ...) :

Public class Boss extends Leader {@ Override public int limit () {return Integer. MAX_VALUE ;}@ Override public void handle (int money) {System. out. println ("the boss approves reimbursement" + money + "Yuan") ;}@ Override public String getLeader () {return "current boss ";}}

Initiate application:

Public class Client {public static void main (String [] args) {// construct the GroupLeader groupLeader = new GroupLeader (); Director dire= new director (); manager manager = new Manager (); Boss boss Boss = new Boss (); // you can specify groupLeader as the processor. nextHandler = director; director. nextHandler = manager; manager. nextHandler = boss; // initiate a report application groupLeader. handleRequest (8000 );}}

Result:

Currently, the team lead has insufficient reimbursement quota. The team lead has insufficient reimbursement quota. The team lead has a manager who approves reimbursement of 8000 RMB.

The responsible chain mode is flexible. Request initiation can start from any node of the responsible chain or change internal transmission rules. For example, if the supervisor is absent, we can directly transfer the supervisor from the leader to the manager.

There are two actions for a handler object in the responsibility chain. One is to process the request, and the other is to pass the request to the next node. A handler object is not allowed to send the request to the previous node after processing the request.

For a chain of responsibilities, there are only two scenarios for a request. One is processed by a processing object, and the other is that all objects are not processed.Pure responsibility chain model, The latter isNon-pure responsibility chain. In reality, most of them are pure responsibility chains.

4. responsibility chain mode in Android Source Code 1. View event distribution and processing

Recursive call of ViewGroup event shipping is similar to a chain of responsibility. Once it finds the responsible person, it will be held by the responsible person and consume the event, it is embodied in the setting of the return value in the onTouchEvent method of View. If false is returned, it means that the current View will not be the owner of this time and will not be held by it; if true is returned, in this case, the View will hold the event and will not pass it out.

5. Summary 1. Advantages

The relationship between the requester and the processor can be decoupled to improve code flexibility.

2. Disadvantages

Each time, we need to traverse request handlers in the chain. If there are too many handlers, traversal will definitely affect performance, especially in some recursive callers.

Related Article

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.