In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "what is the Java template method pattern". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the Java template method pattern"!
What is the template method pattern
Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Template method pattern (Template Method Pattern): defines the framework of an algorithm in operation while delaying some steps to subclasses. The template method allows subclasses to redefine certain steps of the algorithm without changing the structure of the algorithm. Template method pattern belongs to behavioral pattern, which is mainly implemented by the inheritance mechanism of Java.
The template method, as its name implies, is a template that does things according to the template. The template method encapsulates the execution steps of the method, and the caller only needs to call the template to get things done in the correct order.
Two roles of template method pattern
AbstractClass (abstract class): defines a series of basic operations (PrimitiveOperations), which can be concrete or abstract, each of which corresponds to a step of the algorithm, which can be redefined or implemented in its subclass. At the same time, a template method (Template Method) is defined in the abstract class, that is, the framework of an algorithm is defined, and the execution order of these operations is defined.
ConcreteClass (concrete subclass): a subclass of an abstract class that implements abstract basic operations declared in the parent class to complete the subclass-specific algorithm, or overrides concrete basic operations that have been implemented in the parent class.
Template method pattern UML diagram
Template method pattern code demonstration
Some friends would like to ask, what is the relationship between the template method pattern and the Morey event? There is no connection between the two. I am observing the attitude of people in the face of this matter and the way in which we express our views. Abstractly again, can each of us act on this matter simply by opening social software, expressing opinions and opinions, and shutting down social software? What's this? When applied to the project, isn't this a template method? Everyone expresses their views according to this template. the difference is that some people use Weibo, some people use Tiger Pop, some people use Twitter, some support Morey, some scold Morey and so on. Let's take a look at the code implementation.
1. Write abstract template classes
Package com.mazhichu.designpatterns.templatemethod;/** function: abstract template class *
* * @ author Moore * @ ClassName Comment. * @ Version V1.0. * @ date 2019.10.16 11:53:02 * / public abstract class Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected abstract void openSocialSoftware (); / * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected abstract void comment (); / * function: specific method *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected void closeSocialSoftware () {System.out.println ("close social software");} / * * function: template method, template method. In order to prevent malicious operations, general template methods add the keyword final, and subclasses cannot be implemented *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / public final void temtemplateMethod () {openSocialSoftware (); comment (); closeSocialSoftware ();}}
2. Write specific classes
Package com.mazhichu.designpatterns.templatemethod;/** function: specific class *
* * @ author Moore * @ ClassName Zhang san. * @ Version V1.0. * @ date 2019.10.16 14:00:09 * / public class ZhangSan extends Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void openSocialSoftware () {System.out.println ("Open Weibo!") ;} / * * function: basic abstract method *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void comment () {System.out.println ("comment: Morey must apologize, Xiao Hua must apologize! fire Morey!") ;}} package com.mazhichu.designpatterns.templatemethod;/** function: specific class *
* * @ author Moore * @ ClassName Li si. * @ Version V1.0. * @ date 2019.10.16 13:59:56 * / public class LiSi extends Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void openSocialSoftware () {System.out.println ("Open Tiger Pop");} / * * function: basic abstract method *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void comment () {System.out.println ("comment: Morey is stupid, WQNMLGB");} package com.mazhichu.designpatterns.templatemethod;/** function: specific class *
* * @ author Moore * @ ClassName James. * @ Version V1.0. * @ date 2019.10.16 13:59:01 * / public class James extends Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void openSocialSoftware () {System.out.println ("Open Twitter");} / * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void comment () {System.out.println ("comment: Xiao Hua should punish Morey!");}}
3. Client call
Package com.mazhichu.designpatterns.templatemethod;public class Client {public static void main (String [] args) {Comment zhangSan = new ZhangSan (); zhangSan.temtemplateMethod (); System.out.println ("-\ n"); Comment lisi = new LiSi (); lisi.temtemplateMethod () System.out.println ("-\ n"); Comment james = new James (); james.temtemplateMethod ();}}
View the running results:
The above is the template method pattern, which is probably the most common and simplest design pattern, the parent class (abstract class) defines the algorithm and the template method, the template method prescribes the order in which the algorithm is executed, and the subclass only needs to implement the abstract algorithm of the parent class. you can get different results according to the established order of the template method, which greatly simplifies the complexity of the subclass.
Hook method
The reason why this is taken out separately is that whether some methods are executed in template methods is optional, that is, not all algorithms in templates need to be executed and can be decided by subclasses. this is what we call "hook method".
A hook method is a method declared in an abstract class, usually with an empty implementation or a default value, and a subclass can implement to override the hook to determine whether a step of the algorithm step should be executed, which is a reverse control.
Let's modify the abstract class and add hook methods.
1. Re-abstract the class and add the hook method
Package com.mazhichu.designpatterns.templatemethod;/** function: abstract template class *
* * @ author Moore * @ ClassName Comment. * @ Version V1.0. * @ date 2019.10.16 11:53:02 * / public abstract class Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected abstract void openSocialSoftware (); / * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected abstract void comment (); / * function: specific method *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / protected void closeSocialSoftware () {System.out.println ("disable social software");} / * * function: template method. In order to prevent malicious operations, general template methods add the keyword final, and subclasses cannot be implemented *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / public final void temtemplateMethod () {openSocialSoftware (); if (isLogin ()) {comment ();} else {System.out.println ("you are not logged in, you cannot comment, you can only see others scold Morey");} closeSocialSoftware () } / * * Hook methods: can be abstract methods, empty implementations or default implementations, and subclasses can be overridden to determine whether the template method executes certain algorithms * @ return * / protected boolean isLogin () {System.out.println ("comment after login--"); return true;}}
2. Write a subclass that overrides the hook method
Package com.mazhichu.designpatterns.templatemethod;public class LaoBao extends Comment {/ * * function: basic abstract methods *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void openSocialSoftware () {System.out.println ("Open Zhihu");} / * * function: basic abstract method *
* * @ author Moore * @ date 2019.10.16 11:53:02 * / @ Override protected void comment () {System.out.println ("I not only want to scold Morey Shaw, but also want to greet Trump");} @ Override protected boolean isLogin () {return false;}}
3. Client call
Package com.mazhichu.designpatterns.templatemethod;public class Client {public static void main (String [] args) {Comment zhangSan = new ZhangSan (); zhangSan.temtemplateMethod (); System.out.println ("-\ n"); Comment lisi = new LiSi (); lisi.temtemplateMethod () System.out.println ("-\ n"); Comment james = new James (); james.temtemplateMethod (); System.out.println ("-\ n"); Comment laobao = new LaoBao (); laobao.temtemplateMethod ();}}
4. View the running results:
From the results, it can be concluded that after adding the hook method, we can reverse control whether some algorithms in the template method in the parent class should be executed, which also increases the flexibility of the template method.
At this point, I believe you have a deeper understanding of "what is the Java template method pattern?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.