IOS Design Pattern Learning (18) Template Method

Source: Internet
Author: User

1 Preface

The template method mode is a very simple design mode in object-oriented software design. The basic idea is to define a "standard" Algorithm in a method in an abstract class. The basic operations called in this method are implemented by the subclass overload. This method becomes a "template ". Because the algorithm defined by the method lacks some special operations.

2. Details

2.1 Brief Introduction

Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows subclass to redefine certain steps of an algorithm without changing the structure of the algorithm.

2.2 When to use

(1) The unchanged part of the algorithm needs to be implemented at one time, and the variable behavior is left to the subclass for implementation.

(2) The common behavior of subclass should be extracted and put into the public class to avoid code duplication. The differences in existing code should be separated into new operations. Then replace these different codes with a template method that calls these new operations.

(3) The extension of subclass needs to be controlled. You can define a template method to call the hook operation at a specific point. Sub-classes can be expanded on these points through hook operations.

2.3 template method call 5 types of operations

(1) specific operations on a specific class or client class;

(2) specific operations on abstract classes;

(3) Abstract operations;

(4) Factory method;

(5) Hook operation.

3 conclusion

The above is all content and I hope it will help you.

 

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.