Template mode, many of us have used, and we all prefer this mode, first look at the class diagramLiar design mode-Class diagramAs a programmer, do not know "lazy", in the author's opinion is not very qualified Oh! When we write the program, we find that many classes go the same process, only a small number of differences, then we need to consider template mode, t
In the process of learning Java, we must have heard the design pattern of the term, in the industry there is such a word, if you can master 23 design patterns, then you are Daniel!Okay, no more nonsense, today I share with you one of the 23 design patterns Template Method Design
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 for the template m
Intention
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template method enables subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.
Scene
The template approach is a very easy to understand design pattern, because it does not have too many structural interleaving, a
Silver Fox supports Visual Express single template design, and provides more than 300 kinds of latest express single background map, can help you to quickly customize personalized templates.
Express Single Template Management
1, can from the main menu "Order Management"-> "Express single template
Mode motiveTemplate method pattern is a basic technique based on inheritance, and the structure and usage of template method pattern are also the core of object-oriented design. In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses.In the
the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm.roles in the patternAbstract class (AbstractClass): The template method is implemented, and the skeleton of the algorithm is defined.Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm.Public
called, the difference here is only the difference in the function name, and the function does not change anything. Below is the test case of our refactored code and the result of the run. As you see from the results, the output of the test cases that we did not use the template method with are consistent. This is what we've often mentioned in the Refactoring series blog to change the structure inside the code without changing the interface that the
Introduction to Template Method Patterns Template method, as the name implies, is the general process of doing some tasks. If there are many self-introduction templates on the Web, the recommendation template, that is, the beginning and end of the content may be similar, and in the middle of the need for customers to modify the use.
First, UML diagramSecond, the conceptTemplate Method Pattern: defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.Third, the descriptionRole:(1) AbstractClass: abstract class, in fact, is an abstract template, defines and implements a
distributed across different classes, it is very inconvenient to maintain.
Flexible. Because of the hook method, the implementation of sub-classes can also affect the operation of the main logic in the parent class. However, while being flexible, child classes affect the parent class and violate the Lee's replacement principle, which also brings risks to the program. This has higher requirements on the design of abstract classes.
When multiple subcla
(); class2.templateMethod(); }}The above completed a simple template method design and application, in fact, the principle is very simple, abstract the parent class defined the basic method and template method, the implementation of the template methods of the specific logic, concrete
1. Overview
Defines the skeleton of an algorithm in an operation, and defers the step to a subclass. Template methods enable subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm.
2. Role in the pattern
2.1 Abstract Class (AbstractClass): The template method is implemented, and the skeleton of the algorithm is defined.
2.2 Concrete Class (Concreteclass
The book "Design Patterns" describes this in the template method model:
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The steps to redefine the algorithm without changing its structure.
My understanding: Defining an abstract class or interface, defining some abstract methods within it (for Templatemethod calls) and a Templatemethod method (not abstract method
as the data is allocated to the template to use, Regardless of the type of data, all use the Assign () method of the Smarty object to complete. Although the syntax for using various types of variables in the template is similar to that of PHP, it is more complex to design various types of data when designing a template
(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 for the template method pa
IntroductionReferring to the template, you can not help but think of life "CV template", "Paper template", "Word in the template", etc., in real life, the concept of templates is-there is a prescribed format, and then everyone may according to their own needs or circumstances to update it, such as resume
by subclasses, so subclasses can extend the function in a way that conforms to the opening and shutting principle.Disadvantages:According to our design habits, abstract class is responsible for declaring the most abstract, most general things properties and methods, implementation of the class to complete specific things properties and methods, but the template method mode is reversed, the abstract class d
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.