I. Hook (abstract) function

Source: Internet
Author: User

After reading the video of Gao teacher, I summarize the knowledge.

The so-called hook is the interface used to join two things. If two things appear at different times, one side will set aside space, giving the other party the ability to fill the space with entities in the future, and the two are close together.

The hook function is the interface between the Android frame and the application category. Since the Android class inherits the Overridable function as the main hook function.

C + + Examples:

Ex02-01.cpp#include <iostream>using namespace Std;
Class <strong>salesperson</strong>{protected:double Total_amt;public:salesperson (Double A): Total_amt ( A) {}virtual double <strong><span style= "color: #ff6666;" >bonus () </span></strong> const = 0;};
Class <strong>SalesEngineer</strong>: Public <strong>SalesPerson</strong> {public: Salesengineer (Double A): Salesperson (a) {}virtual double <strong><span style= "color: #ff6666;" >bonus () </span></strong> const{return (Total_amt * 0.008 + 500.0);}};
Class <strong>SalesManager</strong>: Public <strong>SalesPerson</strong> {public: SalesManager (Double A): Salesperson (a) {}virtual double <strong><span style= "color: #ff6666;" >bonus () </span></strong>const{return (Total_amt * 0.008 + 2000.0);}}; void Compute_bonus (const salesperson *PS) {cout << "bonus:" << ps->bonus () << Endl;}
int main () {SalesManager Peter (20000.0); Salesengineer Alvin (80000.0), Lily (50000.0); Compute_bonus (&peter); Compute_bonus (&alvin); Compute_bonus (&lily); System ("PAUSE"); return exit_success;}
The bonus () virtual function of the salesperson class plays the role of Hook as an interface between the parent and child categories.

Java Example:

Public abstract Classsalesperson {protected Doubletotal_amt;publicsalesperson (doublea) {Total_amt = A;} public abstract Doublebonus ();}

Public Classsalesmanagerextendssalesperson {Publicsalesmanager (Doublea) {super (a);} Public Doublebonus () {Returntotal_amt * 0.008 + 2000.0;}}

Public Classjmain {static Voidcompute_bonus (salesperson PS) {System.out.println ("Bonus:" + Ps.bonus ());} public static Voidmain (string[] args) {SalesManager peter = new SalesManager (20000); Salesengineer Alvin =newsalesengineer (80000); Salesengineer Lily = new Salesengineer (50000); Compute_bonus (Peter); Compute_bonus (Alvin); Compute_bonus (lily);}

This is like the above C + +, Salespersons's bonus () is the hook function.

Take a look at the use of hook functions in Android:

To render the ListView window again, the following three classes are used:


In these classes, there are many hook functions, activity OnCreate (), Baseadapter's GetView (), and so on.

Since the abstract design of the framework is preceded by the application category, the designer of the design framework does not know what the app's ListView looks like when composing the activity, the ListView, and the Baseadapter, so the void is reserved for the application to fill,


When the application executes, the framework grasps the entire control and invokes the OnCreate function of the myactivity subcategory.


The hook function implements the IOC.

The ListView is reversed from Baseadapter when the activity requires the contents of the ListView to be displayed

Calls Mystringadapter's GetView and other functions, and obtains the displayed content.

The hook function realizes IOC, promotes the actual situation to depend on, therefore, it occupies the very core position in the frame design.


The relationship between hook function and pattern

hook function is the interface between parent and child classes, there are many abstract (unchanging) parent-child categories in the application framework, which need hook function to converge,

Factory method Mode:

where FactoryMethod () is the hook function,


Template method Mode:


where Promitiveoperation () is the hook function,




I. Hook (abstract) function

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.