In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the template pattern in Java. I hope you will get something after reading this article. Let's discuss it together.
Advantages
Encapsulate the invariant part and extend the variable part.
Extract common code for easy maintenance.
The behavior is controlled by the parent class and implemented by the subclass.
Shortcoming
Each different implementation needs a subclass to implement, which leads to an increase in the number of classes and makes the system larger.
Working with scen
1. There are methods common to multiple subclasses and the logic is the same.
two。 Important and complex methods can be considered as template methods.
I. the way of realization
Suppose a scene, when we are playing the game, we all need to initialize and load the game, then start the game, and finally end the game, which is like a set of templates, but the specific implementation methods are different, for example: the loading resources of LOL games and CF games are definitely different, and the content of the game is also different.
1. Game abstract class package com.asurplus.common.template;/** * Game abstract class * / public abstract class Game {/ / initialize the game abstract void init (); / start the game abstract void start (); / / end the game abstract void end (); / / template public final void play () {/ / initialize the game init () / / start the game start (); / / end the game end ();}} 2, LOL games package com.asurplus.common.template;import lombok.extern.slf4j.Slf4j;/** * LOL games * / @ Slf4jpublic class LolGame extends Game {@ Override void init () {log.info ("initialize LOL games") } @ Override void start () {log.info ("LOL Game start");} @ Override void end () {log.info ("LOL Game end");}} 3, CF Game package com.asurplus.common.template;import lombok.extern.slf4j.Slf4j / * CF Game * / @ Slf4jpublic class CfGame extends Game {@ Override void init () {log.info ("initialize CF Game");} @ Override void start () {log.info ("CF Game start");} @ Override void end () {log.info ("CF Game end");}} II. Test package com.asurplus.common.template / * template mode * / public class TestMain {public static void main (String [] args) {/ / LOL game Game lolGame = new LolGame (); lolGame.play (); System.out.println (); / / CF game Game cfGame = new CfGame (); cfGame.play ();}}
Output result
As you can see, a set of templates can be implemented in multiple ways, thus implementing our factory pattern.
After reading this article, I believe you have a certain understanding of "how to use template mode in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.