Template method Mode:defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. This pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. Usage scenarios:1, one-time implementation of an invariant part of the algorithm, and the variable behavior left to the subclass to achieve. 2 . The public behavior of
Template mode
The template design pattern creates an abstract object that implements a set of methods that the subclass typically uses as a template for its own design.
Design Scenarios
Typically used for database abstraction classes.
Code Design:
[PHP]
if (!defined (' is_initphp ')) exit (' Access denied! ');
/****
Template Methodology Design pattern (Templete method)Defines an algorithm skeleton in an operation, and delays the implementation of some mutable parts into subclasses.The template method design pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.1 I
Abstract
Template method I learned the second pattern, which is a very easy to understand pattern, but very practical.
Intent
For operation, only the outline of the algorithm is defined first, and some steps are left to the subcategory to fill, so that the subcategories go through some steps without changing the overall architecture of the algorithms.
Its UML
template ModeProblem ScenarioCoffee and tea are derived from abstract drinks, coffee and tea have the method of boiling water, so you can boil water to extract the method to the abstract beverage to achieve, and coffee with a coffee powder to the cup method, the tea set has a way to add tea to the cup, it looks like two methods are different logic, Abstract classes are referenced in many places by other types, that is, other types call abstract classe
/* Requirements: Gets the time that a program runs. Principle: Get the program start and end time and subtract. Get time: System.currenttimemillis (); When the code is optimized, you can solve this kind of problem. This way, the template method design pattern. What is a template method? When defining a feature, part of the feature is deterministic, but part of it
The core idea of the template method pattern is to move the invariant behavior to the superclass and remove the duplicated code in the subclass to reflect its advantages. In fact, the template method pattern is to provide a good code reuse platform.
First, let's take a quick look at the definition:
template method determines their sequencing, but it allows the application and document subclasses to change these steps to meet their needs. 3. Applicability one-time implementation of the invariant part of the algorithm, the variable part to the subclass implementation of the common behavior of each subclass should be extracted and centralized into the parent class to avoid the extension of the code repeating subclass by the parent class control 4.
in speech recognition, the same words are the same person, each time the situation is different, difficult to identify. This chapter is a definition of how to adapt to different situations with different characteristic indicators.1, based on the best path search measure: ① the Bayesian optimality principle and dynamic programming ② editing distance (the edit Distance) ③ in speech recognition dynamic time warp (DTW), speaker-dependentrecognition. Speaker-independentrecognition.2, correlation-base
2018-05-06Template Method Design patternThe skeleton of an algorithm in an operation is defined in a method of the parent class, and some steps are deferred to the subclass because the implementation details of the different subclasses are different. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.Abstract Parent class: defines the business skeleton in the operation,
DefinedTemplate method Pattern: Defines the framework for an algorithm in an operation, and delays some steps into subclasses. The template method pattern allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.RealizeTemplate class Public Abstract classDrawperson { Public Abstract voidDrawhead (); Pu
Template templates Schema Definition:Defines the skeleton of an algorithm in an operation, delaying execution of some steps into its subclasses. With Java's abstract classes, it is often used in template mode, so template mode usage is common. And it's easy to understand and use. Public abstract class benchmark{/** * The following action is what we want to comple
For speech recognition, the same word is said differently by the same person each time, which brings difficulties to recognition. This chapter discusses how to define metrics that adapt to different features in different situations.
1. Measure Based on Optimal Path search: ① bellman's optimum principle and Dynamic Programming ② the edit distance ③ In the speech recognition Dynamic Time distortion (DTW), speaker-dependentrecognition. speaker-independentrecognition.
2. Correlation-based meas
Template method Mode
English name
Template Method Pattern
definition
The skeleton of an algorithm is defined in a method, and some steps are deferred to subclasses, and the template method allows subclasses to redefine some of the steps in the algorithm without altering the algorithm structure.
Principles
H
In my design Journey: Strategy pattern (primary) (C + +) (OO C + +) (Template C + +), we used strategy pattern to let graph Er can draw triangle, circle and square
Because the need to change again,:D, we hope that Grapher will be able to print the text in each shape, the implementation of the results are as follows
Draw Hello Shape!! in Square Draw Hello C++!
Banktemplatemethod Abstract classPublic abstract class Banktemplatemethod {//Concrete method public void Takenumber () {System.out.println ("queue");} public abstract Void Transact (); Transact specific business//hook method public void Evaluate () {System.out.println ("Feedback Score");} Public final void process () {//Template Method!!! This.takenumber (); This.transact (); This.evaluate ();}}Test class Clientpublic class Client {public static void
(1) Template method definition:When defining a feature, part of the feature is deterministic, but part of it is indeterminate, and the identified part is using the indeterminate part,Then the uncertain parts are exposed. It is done by subclasses of the class.(2) Sample code:1 //get the execution time of a method2 Abstract classGetTime3 {4 Public Final voidgetTime ()5 {6 LongStart =System.currenttimemillis ();7 8 RunCode ();9 Ten
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.