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
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
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
(); 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
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
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
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
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
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
defines the skeleton of the algorithm. (2) concrete templates (concrete template): The concrete template is the subclass of the abstract template, which implements the primitive operation in the abstract template. The class diagram for the template method pattern structure
Recently intends to consolidate and organize the design patterns related content. This article is about--template mode!Original: http://www.jb51.net/article/76052.htm--------------------------------------------------------------------------------------------------------------- -----------------------------------This article mainly introduces the template method p
Note This blog excerpt from Qin Xiaobo teacher wrote "design pattern of Zen"Template method Mode car factory made HummerSuppose we were a car company, and now we have customers coming and asking us to build Hummer! Since the superior down orders that will be built, but build Hummer you have to tell us what the car function ah, the customer said: "Can start the car, can stop the car, can ring, can run." "Wel
- housetype.buildhouse (); - } the -}The output of the program is as follows:Build FoundationBuilding Wooden WallsBuilding Pillars with Wood coatingBuild windowsHouse is built.************Build FoundationBuilding Glass WallsBuilding Pillars with glass coatingBuild windowsHouse is built.In the abstract parent class we define buildhouse, a template method that defines the skeleton of the algorithm and defers execution of Buildpillars and buildwal
(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
logic represented by the template method.Whenever you define a new subclass, you should consider which operations must be replaced, which operations are replaceable, and which operations cannot be replaced. Using template patterns can make these responsibilities clear.
pros and Cons:
Advantages:1 The template method pattern defines the algorithm formally in a c
"Component Collaboration" mode:#现代软件专业分工之后的第一个结果是 "Framework-to-application partitioning", the "component collaboration" mode, which uses late binding to achieve loose coupling between the framework and the application, is a common pattern in collaboration between the two.#典型模式: Template Method, Strategy, observer/eventPart 1 Template Method template mode Motive
Author: gnuhpcSource: http://www.cnblogs.com/gnuhpc/
1. This mode is relatively simple. Here is an example of a bank loan application process (This is not an example in the book.):
When applying for a loan, the Bank should check the customer's affairs, for example, the customer's revenue and expenditure records, his credit records obtained from three places, other existing debt information, the stock market value of the borrower, and the expected future income analysis of the borrower. We can
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.