The distribution mechanism of the Android touch event Click event Three---responsibility chain model

Source: Internet
Author: User

The previous two sections describe the distribution process for Android click events. In fact, we can carefully understand that this distribution process is always from the top floor to the lower level. One layer at a level in order.

Of course, to which layer to stop, we can do it by rewriting some methods.

This is where Android developers are using the responsibility chain model to do this code. Let's take a look at what the responsibility chain model is. And how to use it.

As you know, the basic architecture of a software company is that the programmer----leader---project Manager---boss's infrastructure.

We usually have teambuilding, right ~ ~, for example, to finish a small project programmer Xiao Zhang and leader proposed we building once, apply for a number of funds.

However, the general leader only the authority to reply about 1000 of the funds, if more than will find the manager to approve, of course, Manger is also authorized, can only be approved under 3000

More than 3000 of them can only get approval from the boss.

This is the typical chain of responsibility model. Let's show this life's scene in code, and you'll know how to use the chain of responsibility. ~~~~~

Define a virtual class first, and this class is our base class.

1  Packagecom.design.test;2 3  Public Abstract classHandler {4 5     PrivateHandler successor =NULL;6 7      PublicHandler getsuccessor () {8         returnsuccessor;9     }Ten  One      Public voidSetsuccessor (Handler successor) { A          This. successor =successor; -     } -  the      Public Abstract voidHandlerrequest (String usr,intMoney ); -  -}

Then define a leader. We assume that this leader has only 1000 approval authority.

1  Packagecom.design.test;2 3  Public classLeaderextendsHandler {4 5 @Override6      Public voidHandlerrequest (String usr,intMoney ) {7         if(Money > 1000) {8System.out.println ("Leader does not have permission to approve" +usr9+ "The application for this activity is more than 1000 yuan to find PM application went");Ten getsuccessor (). Handlerrequest (usr, money); One  A}Else { -System.out.println ("leader approved" + USR + "Application for this event funding total" + Money -+ "Yuan"); the         } -  -     } -  +}

And then define a manager to see if he has 3000 approval authority.

1  Packagecom.design.test;2 3  Public classManagerextendsHandler {4 5 @Override6      Public voidHandlerrequest (String usr,intMoney ) {7         //TODO auto-generated Method Stub8         if(Money > 3000) {9System.out.println ("Manager does not have permission to approve" +usrTen+ "The funding for this event is more than $3000 for boss applications."); One getsuccessor (). Handlerrequest (usr, money); A}Else { -System.out.println ("Manager approved" + usr + "Request for this activity, total" + Money -+ "Yuan"); the  -         } -  -     } +  -}

Finally we define a boss, since is the boss, the authority of course is their own, arbitrarily approved no limit, haha

1  Packagecom.design.test;2 3  Public classBossextendsHandler {4 5 @Override6      Public voidHandlerrequest (String usr,intMoney ) {7         //TODO auto-generated Method Stub8 9System.out.println ("Boss approved" + usr + "Application for this event funding total" + Money + "Yuan");Ten  One     } A  -}

And then we'll test the chain of responsibility. Write a Main class

 Packagecom.design.test; Public classTestmain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubHandler leader =NewLeader (); Handler Manager=NewManager (); Handler boss=NewBoss ();        Leader.setsuccessor (manager);        Manager.setsuccessor (boss); Leader.handlerrequest ("Zhang San", 800); System.out.println ("----------------------"); Leader.handlerrequest ("Zhang San", 1200); System.out.println ("----------------------"); Leader.handlerrequest ("Zhang San", 3200); }}

Last look at the log

Leader approved the Zhang San application for a total of $800
----------------------
Leader did not have permission to approve the Zhang San application for this activity because more than 1000 yuan to find PM application went
The Manager approved the Zhang San application for the event, a total of 1200 yuan
----------------------
Leader did not have permission to approve the Zhang San application for this activity because more than 1000 yuan to find PM application went
The Manager does not have permission to approve the funding for the Zhang San application because it's more than $3000 for the boss application.
Boss approved the Zhang San application of this activity funds a total of 3200 yuan

This way, if you understand the code, it's a good idea. In fact, this mode is used in many scenes. For example, the filter in Tomcat is done using this pattern.

The distribution mechanism of the Android touch event Click event Three---responsibility chain model

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.