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,
How to implement the template method in the design pattern in JS? The generation of ideas will inevitably require familiarity with JS, how to achieve? is very simple, all know that in JS if you define two methods with the same name, the previous method will be overwritten by the latter method, using this feature to implement the template method.For example, in a
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
Design Pattern-Template MethodDescription:Define the skeleton of an algorithm in a method. Delay some steps to the subclass. The template method allows the subclass to redefine some steps in the algorithm without changing the algorithm structure.HOOK: defines an empty method or default method, and provides the user subclass to overwrite and implement its own judg
模板方法这个名字看着很陌生,其实在游戏中大量地使用了模板方法,因为游戏中存在玩家、NPC和静态物体等不同的对象,使用多态特性能很好地进行区分。 模板方法的定义是:模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。举个最简单的玩家和NPC死亡时的逻辑:流程图如下:The code is as follows//MVC.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include #include #include using namespace STD;classobject{ Public: Object (): M_dead (0) { }voidSetdead () {cout"========================="cout"========================="Virtual voidBeforedead () {}Virtual voi
Description: Defines an algorithm skeleton in an operation, delaying some of the steps into subclasses. Prepare a draw template, implement part of the logic, and then declare some common abstract methods that require different implementations for subclasses.
Scenario: For multiple businesses, many functions in their business are similar, and some functions need to be implemented on their own. This allows us to use
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
First on the codevar formatString = function (str,data) {return str.replace (/\{# (\w+) #\}/g,function (Match,key) {return typeof Data[key] = = = undefined? ": Data[key]});} Self.each (function () {_this = $ (this);//_html = ' Mainly used in the application of string splicing, this can reduce the previous frequent splicing errors, the format is more clear.How to apply a template pattern in existing code
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.