Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the template method in web design pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the template method in web design pattern". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is template method pattern template method pattern 1. The skeleton of the algorithm in an operation is defined, and some of the steps are implemented in the subclass. The template method pattern allows subclasses to redefine certain steps of an algorithm without changing the structure of the algorithm. two。 Template method pattern is one of the most common patterns in all schemas. It is the basic technology of code reuse based on inheritance and has no association relationship. Therefore, in the class structure diagram of the template method pattern, there are only inheritance relationships. Core design points AbstractClass: abstract classes that define and implement a template method. This template method defines the skeleton of the algorithm, and the logical composition step in the corresponding abstract operation is deferred to the subclass to implement ConcreteClass: to implement one or more abstract methods defined by the parent class. Application scenario of template method

For example, system callback code refactoring in aggregate payment platform

Servlet request

Template method code implementation template method abstract class @ Slf4j@Componentpublic abstract class AbstractPayCallbackTemplate {/ * asynchronous callback business * * @ return * / public String asyncCallBack () {/ / 1. Payment callback verification parameter Map verifySignatureMap = verifySignature (); / / 2. Parameter verification is successful and is written to the log.. PayLog (verifySignatureMap); String analysisCode = verifySignatureMap.get ("analysisCode"); if (! analysisCode.equals ("200")) {return resultFail ();} / / 3. Execute the callback asynchronous correlation logic return asyncService (verifySignatureMap);} / * payment callback verification parameter * * @ return * / protected abstract Map verifySignature () / * * use multithreading to write to the log asynchronously * * @ param verifySignatureMap * / @ Async private void payLog (Map verifySignatureMap) {log.info ("> second step write payLog.") } / * * each subclass needs to implement the business resolution operation * * @ return * / protected abstract String asyncService (Map verifySignatureMap); / * return the result asynchronously. * * @ return * / protected abstract String resultSuccess (); / * * Asynchronous return failure * * @ return * / protected abstract String resultFail ();} specific implementation template

AliPayCallbackTemplate

@ Slf4j@Componentpublic class AliPayCallbackTemplate extends AbstractPayCallbackTemplate {@ Override protected Map verifySignature () {/ / > assume that the callback message for Alipay > log.info ("> the first step parses the payment message .verifySignature ()"); Map verifySignature = new HashMap (); verifySignature.put ("price", "1399"); verifySignature.put ("orderDes", "recharge Ant classroom permanent member") / / A payment status of 1 indicates success. VerifySignature.put ("aliPayMentStatus", "1"); verifySignature.put ("aliPayOrderNumber", "201910101011"); / / whether the parsing message is successful. VerifySignature.put ("analysisCode", "200"); return verifySignature;} @ Override protected String asyncService (Map verifySignatureMap) {log.info ("> step 3 asyncService () verifySignatureMap: {}", verifySignatureMap); String paymentStatus = verifySignatureMap.get ("aliPayMentStatus"); if (paymentStatus.equals ("1")) {String aliPayOrderNumber = verifySignatureMap.get ("aliPayOrderNumber") Log.info ("> orderNumber: {aliPayOrderNumber}, successfully modified the order status to already paid...);} return resultSuccess ();} @ Override protected String resultSuccess () {return" ok ";} @ Override protected String resultFail () {return null;}

UnionPayCallbackTemplate

@ Slf4j@Componentpublic class UnionPayCallbackTemplate extends AbstractPayCallbackTemplate {@ Override protected Map verifySignature () {/ / > suppose it is UnionPay callback message > log.info ("> first step parse UnionPay data message. VerifySignature ()"); Map verifySignature = new HashMap (); verifySignature.put ("price", "1399"); verifySignature.put ("orderDes", "recharge Ant classroom permanent member") / / A payment status of 1 indicates success. VerifySignature.put ("paymentStatus", "1"); verifySignature.put ("orderNumber", "201910101011"); / / whether the parsing message is successful. VerifySignature.put ("analysisCode", "200"); return verifySignature;} @ Override protected String asyncService (Map verifySignatureMap) {log.info ("> step 3 asyncService () verifySignatureMap: {}", verifySignatureMap); String paymentStatus = verifySignatureMap.get ("paymentStatus"); if (paymentStatus.equals ("1")) {String orderNumber = verifySignatureMap.get ("orderNumber") Log.info ("> orderNumber: {orderNumber}, successfully modified the order status to already paid...);} return resultSuccess ();} @ Override protected String resultSuccess () {return" success ";} @ Override protected String resultFail () {return null }} factory mode acquisition template @ Componentpublic class TemplateFactory {public static AbstractPayCallbackTemplate getPayCallbackTemplate (String templateId) {AbstractPayCallbackTemplate payCallbackTemplate = (AbstractPayCallbackTemplate) SpringUtils.getBean (templateId); return payCallbackTemplate;}} controller@RestControllerpublic class TemplateController {@ RequestMapping ("/ asyncCallBack") public String asyncCallBack (String templateId) {AbstractPayCallbackTemplate payCallbackTemplate = TemplateFactory.getPayCallbackTemplate (templateId); return payCallbackTemplate.asyncCallBack () }} console output result > first step parse payment treasure message. VerifySignature () > second step write payLog. > third step asyncService () verifySignatureMap: {aliPayOrderNumber=201910101011, orderDes= recharge Ant classroom permanent member, price=1399, analysisCode=200, aliPayMentStatus=1} > orderNumber: {aliPayOrderNumber}, has been paid successfully to change the order status to already paid. 1.) advantage template method mode by moving the immutable behavior to the superclass The duplicate code in the subclass is removed. The subclass implements some details of the algorithm, which is helpful to the expansion of the algorithm. The operation implemented by a subclass is called by a parent class, and a new behavior is added through the subclass extension, which conforms to the "open-closed principle". 2.) the disadvantage is that each different implementation needs to define a subclass, which leads to an increase in the number of classes and a more abstract design. 3.) applicable scenarios in some kinds of algorithms, the same method is used, resulting in code duplication. Control subclass extension, subclass must abide by the algorithm rules. "how to use the template method in the web design pattern" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report