Android and design mode-Template method mode

Source: Internet
Author: User

in the book "Java and Patterns" of Dr. Shanhong, this describes the template method pattern:

  The template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern.

Structure of the template method pattern
The template method pattern is one of the most common patterns in all patterns and is the basic technique for code reuse based on inheritance.
The template method pattern requires collaboration between the designer of the abstract class and the specific subclass. A designer is responsible for giving the outline and skeleton of an algorithm, while others are responsible for the various logical steps of the algorithm. The methods that represent these specific logical steps are called basic methods (primitive method), and the method of summarizing these basic methods is called template method, and the name of the design pattern is from here.
The behavior represented by a template method is called a top-level behavior, and its logic is called top-level logic. The static structure diagram of the template method pattern is shown below, with Android view as an example:


There are two characters involved:
Abstract template Role ( in fact not necessarily abstract class, as in this case view) has the following responsibilities:
One or more abstract operations are defined to allow subclasses to implement. These abstractions are called basic operations, and they are the constituent steps of a top-level logic.
Defines and implements a template method. This template method is generally a concrete method, it gives a top-level logic of the skeleton, and the logical composition of the steps in the corresponding abstract operation, deferred to the sub-class implementation. It is also possible for top-level logic to invoke some concrete methods.


The specific template (concrete templates) role has the following responsibilities:
Implement one or more of the abstract methods defined by the parent class, which are the constituent steps of a top-level logic.
Each abstract template role can have any number of specific template roles corresponding to it, and each specific template role can give a different implementation of these abstract methods (that is, the constituent steps of top-level logic), so that the implementation of the top-level logic varies.


The key to template mode is that subclasses can displace the mutable parts of the parent class, but subclasses cannot change the top-level logic represented by the template method.


Whenever a new subclass is defined, do not think in terms of the flow of control, but in accordance with the "responsibility" thinking. In other words, you should consider which operations must be replaced, which operations can be displaced, and which actions are not replaceable. Using template mode can make these responsibilities clear.

Methods in the template method pattern
The methods in the template method can be divided into two main categories: Template method and Basic method.


Template method
A template method is defined in an abstract class that combines basic operational methods to form a total algorithm or a method of total behavior.
An abstract class can have any number of template methods, not limited to one. Each template method can invoke any number of specific methods.

Basic methods
The basic method can be divided into three kinds: abstract method, Concrete method (concrete methods) and hook method.

Abstract method: An abstract method is declared by an abstract class and implemented by a specific subclass. In the Java language, abstract methods are indicated by the Abstraction keyword.

Concrete method: A concrete method is declared and implemented by an abstract class, and the subclass is not implemented or replaced.

Hook method: A hook method is declared and implemented by an abstract class, and subclasses are extended. Usually the implementation given by an abstract class is an empty implementation, as the default implementation of the method.

Default Hook method
A hook method is often given by an abstract class to an empty implementation as the default implementation of this method. This empty hook method is called "Do nothing Hook". Obviously, this default hook method has already been seen in the default adaptation mode, and a default adaptation mode is that a class provides a default null implementation for an interface, so that the subclass of the default adaptation class does not have to give the implementation of all the methods as the implementation of the interface, because usually a concrete class does not need all the methods. (Text from the network, reproduced)


Source:

public class View implements Drawable.callback, Keyevent.callback, Accessibilityeventsource {.../** * Im     Plement this to do your drawing.    * * @param canvas the canvas on which the background'll be drawn */protected void OnDraw (canvas canvas) {     } .../** * Manually render this view (and all of it children) to the given Canvas.  * The view must has already do a full layout before this function is * called.     When implementing a view, implement * {@link #onDraw (Android.graphics.Canvas)} instead of overriding this method.     * If You don't need to override this method, call the superclass version.     * * @param canvas the canvas to which the View is rendered.        */public void draw (canvas canvas) {... onDraw (canvas);        ... dispatchdraw (canvas);        ... ondrawscrollbars (canvas); ......    } .../** * <p>request the drawing of the horizontal and the vertical ScrollBar. The * scrollbars is painted only if they has been awakened first.</p> * * @param canvas the canvas on Which to draw the scrollbars * * @see #awakenScrollBars (int) */protected final void Ondrawscrollbars (Canva S canvas) {...} .../** * Called by draw to draw the ' child ' views. This is overridden * by derived classes to gain control just before its children is drawn * (but after its OW     N view has been drawn). * @param canvas the canvas on which to draw the view */protected void Dispatchdraw (canvas canvas) {} ...}

public class TextView extends View implements Viewtreeobserver.onpredrawlistener {    @Override    protected void OnDraw (canvas canvas) {        ....//textview. ...        Super.ondraw (canvas);        ......    }}



Android and design mode-Template method mode

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.