Android design mode Series-Template method mode

Source: Internet
Author: User

The template method, and the singleton mode is the simplest of the two modes that I think Gof 23.
But I am personally very much in the classical thinking of template methods, although the template method is not recommended in the case of large logarithm, but this method of calling subclasses by the parent class, using inheritance to change part of the algorithm is a basic understanding of object-oriented.
For example, the father has a lot of ideals, medicine to save people, but the father of medicine, can only rely on son, son grew up following father ambition, Spring Breeze, Miaoshouhuichun, realize the father's ideals, son do things early in the birth of the set up, is the father before the long-established template.
Recognizing the idea of a template approach, the parent class can make the unknown subclass do something that it might not do well or simply do, and help frame learning.
In this paper, the draw method in view is used as an example to expand the analysis.
The template method, Templatemethod, is a pattern of learning this pattern that will have a long-term impact on you.

1. Intentions
The skeleton of an algorithm in an operation is defined, and some steps are deferred to subclasses, and the template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Hot words: Skeleton step structure delay to subclass

2. Structure

Defines several steps, such as three-way, to perform these steps in a certain structure order in the template method. A method of the parent class can have a default implementation, or it can be an empty implementation, known as a hook operation.
In combination with the actual situation, we draw a few steps related to the Draw method in view as follows:


Learning Template method is very helpful for us to understand the framework's base class implementation, life cycle and process control, and I think it is a pattern that must be mastered.

3. Code

  1. Public class view{
  2. /**  
  3. * Hook operation, empty implementation
  4. */
  5. protected void OnDraw (canvas canvas) {
  6. }
  7. /**  
  8. * Hook operation, empty implementation
  9. */
  10. protected void Dispatchdraw (canvas canvas) {
  11. }
  12. //Algorithm skeleton
  13. public void Draw (canvas canvas) {
  14. if (!verticaledges &&!horizontaledges) {
  15. //Step 1
  16. if (!dirtyopaque) OnDraw (canvas);
  17. //Step 2
  18. Dispatchdraw (canvas);
  19. //Step 3
  20. Ondrawscrollbars (canvas);
  21. return;
  22. }
  23. }
  24. //... ...  
  25. }

Let's look at the implementation of the system component TextView:

    1. Public class textview{
    2. @Override
    3. protected void OnDraw (canvas canvas) {
    4. //A large number of custom implementation codes
    5. }
    6. }

If we customize the view, we can also generally rewrite the OnDraw method:

  1. Public class MyView extends View {
  2. Public MyView (context context) {
  3. super (context);
  4. }
  5. @Override
  6. protected void OnDraw (canvas canvas) {
  7. Super.ondraw (canvas);
  8. }
  9. @Override
  10. protected void Dispatchdraw (canvas canvas) {
  11. Super.dispatchdraw (canvas);
  12. }
  13. }

4. Effects
(1). The template method is a basic technique for code reuse. They are particularly important in class libraries, which extract public behavior from the class library.
(2). The template method leads to a directional control structure, the "Hollywood Rule": "Don ' t call Me,i will calls you.", that is, a parent class invokes the operation of a subclass, not the other way around.
(3). The type of the template invoke operation has specific operations, specific abstracclass operations, primitive operations, factory methods, hook operations. The primitive operation is less defined.
(4). The name of these redefined actions in Android likes to prefix the method with a suffix on.
(5). The template method uses inheritance to change part of the algorithm. The policy pattern uses delegates to change the entire algorithm.

Android design mode Series-Template method mode

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.