Android Design Pattern series (4)-template method pattern of SDK source code

Source: Internet
Author: User

The template method and Singleton mode are the simplest two modes in my opinion in gof 23.
However, I personally admire the classic idea of the template method. Although the template method is not recommended in the case of a large logarithm, This method uses inheritance to change the method of calling subclass through the parent class.AlgorithmIs a basic understanding of object-oriented.
For example, if my father has many ideals, you can practice medicine to save lives. However, if my father does not have good medical skills, he can only rely on his son. When his son grows up, he complies with his father's ambition, what the son did was settled before he was born. It was a template set by his father for a long time.
Recognizing the idea of the template method, the parent class can let the unknown subclass do what it may or cannot do at all, which is of great help to the Framework learning.
This article uses the draw method in view as an example to analyze it.
The template method, templatemethod. Simply learning this mode will have a long-term impact on you.

1. Intention
Defines the skeleton of an algorithm in an operation and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of the Algorithm without changing the structure of an algorithm.
Popular Words: skeleton step structure delay to subclass

2. Structure

Several steps 1, 2, 3 are defined, and these steps are executed in a certain structural order in the template method. The method of the parent class can have a default implementation, or it can be an empty implementation, that is, a hook operation.
Based on the actual situation, the following steps are involved in drawing the draw method in the View:


The Learning template method is very helpful for us to understand the basic class implementation of the Framework, lifecycle and process control. I think it is a necessary model.

3.Code

Public Class View {/*** hook operation, empty implement */protected void ondraw (canvas) {}/ *** hook operation, empty implement */protected void dispatchdraw (canvas) {}// algorithm skeleton public void draw (canvas) {If (! Verticaledges &&! Horizontaledges) {// Step 1 If (! Dirtyopaque) ondraw (canvas); // Step 2 dispatchdraw (canvas); // step 3 ondrawscrollbars (canvas); Return ;}}//......}

Let's take a look at the implementation of the system component textview:

 
Public class textview {@ override protected void ondraw (canvas) {// a large number of custom implementation code }}

If we customize the view, we generally rewrite the ondraw method:

Public class myview extends view {public myview (context) {super (context) ;}@ overrideprotected void ondraw (canvas) {super. ondraw (canvas) ;}@ overrideprotected void dispatchdraw (canvas) {super. dispatchdraw (canvas );}}

4. Results
(1). The template method is a basic technology for code reuse. They are particularly important in the class library. they extract the public behavior in the class library.
(2 ). the template method leads to a direction control structure, "Hollywood law": "Don't call me, I will call you. ", that is, a parent class calls the subclass operation, rather than the opposite.
(3) The template call operation types include specific operations, specific abstracclass operations, primitive operations, factory methods, and hook operations. Less primitive operations.
(4) In Android, the naming of these redefinition operations is like adding a prefix on before the method.
(5) The template method uses inheritance to change part of the algorithm. The rule mode uses delegation to change the entire algorithm.

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.