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

Definition of Java template pattern and Analysis of Application scenario

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the definition of Java template pattern and application scenario resolution". In daily operation, I believe many people have doubts about the definition of Java template pattern and application scene parsing. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Java template pattern definition and application scenario resolution". Next, please follow the editor to study!

The template pattern, as its name implies, is the way of rubbing through the template.

To define templates is to define frames, structures, and prototypes. Define an agreement that we all abide by.

After defining the template, our remaining work is to enrich it, enrich it, and improve its shortcomings.

The definition template is defined by the abstract class, and the common structured logic needs to be completed in the abstract class, and only the non-public part of the logic is abstracted into abstract methods, leaving the subclasses to enrich the implementation.

So the deficiency mentioned above is these abstract methods.

Generally speaking, the template pattern defines a logical template, logical framework and logical prototype through abstract classes, and then abstracts the undecided parts into abstract classes to be implemented by subclasses. generally speaking, the calling logic of these abstract classes is completed in the abstract class. From this point of view, the template is to define a framework, such as building a house, we define a template: the house should be closed, with doors, windows, etc., but what kind of doors, what kind of windows, these are not described in the template, this is left to the subcategory to improve, such as doors using security doors, windows using northward windows, and so on.

Let's take building a house as an example to see how the template model is:

Template abstract class: HouseTemplate

Public abstract class HouseTemplate {protected HouseTemplate (String name) {this.name = name;} protected String name; protected abstract void buildDoor (); protected abstract void buildWindow (); protected abstract void buildWall (); protected abstract void buildBase (); / / Common logic public final void buildHouse () {buildBase (); buildWall (); buildDoor (); buildWindow ();}}

Subclass 1:HouseOne

Public class HouseOne extends HouseTemplate {HouseOne (String name) {super (name);} @ Override protected void buildDoor () {System.out.println (name + "doors with security doors");} @ Override protected void buildWindow () {System.out.println (windows facing north ");} @ Override protected void buildWall () {System.out.println (walls of name +" made of marble ") } @ Override protected void buildBase () {System.out.println (name + "the foundation is made of steel");}}

Subclass 2:HouseTwo

Public class HouseTwo extends HouseTemplate {HouseTwo (String name) {super (name);} @ Override protected void buildDoor () {System.out.println (name + "doors with wooden doors");} @ Override protected void buildWindow () {System.out.println (name + "windows facing south");} @ Override protected void buildWall () {System.out.println (walls of name + "made of glass") } @ Override protected void buildBase () {System.out.println (name + "the foundation uses granite");}}

Test class: Clienter

Public class Clienter {public static void main (String [] args) {HouseTemplate houseOne = new HouseOne ("House 1"); HouseTemplate houseTwo = new HouseTwo ("House 2"); houseOne.buildHouse (); houseTwo.buildHouse ();}}

Test result

The foundation of house 1 is made of steel foundation, the walls of house 1 are built of marble, the doors of house 1 are built with security doors, the windows of house 1 face north, the foundations of house 2 are made of granite, the walls of house 2 are made of glass, the doors of house 2 are made of wooden doors, and the windows of house 2 face south.

Through the above examples, we know the basic methods and template methods in the template pattern, in which the buildHouse method in HouseTemplate is the basic method, and the other four are template methods. The basic method is generally modified with final to ensure that it will not be modified by the subclass, while the template method is modified with protected, indicating that it needs to be implemented in the subclass.

In fact, there is also a concept of hook method in the template pattern, some people say that the template pattern with hook method is complete, maybe.

What do you do with the hook method? Hooks are authorizations to subclasses that allow them to override the execution of basic logic by overriding hook methods, which is sometimes very useful. For example, when building a house, you can do this when you need a subclass to decide whether or not to build a toilet room:

Template abstract class: HouseTemplate

Public abstract class HouseTemplate {protected HouseTemplate (String name) {this.name = name;} protected String name; protected abstract void buildDoor (); protected abstract void buildWindow (); protected abstract void buildWall (); protected abstract void buildBase (); protected abstract void buildToilet (); / / Hook method protected boolean isBuildToilet () {return true;} / / Common public final void buildHouse () {buildBase (); buildWall (); buildDoor (); buildWindow (); if (isBuildToilet ()) {buildToilet ();}

Subclass 1:HouseOne

Public class HouseOne extends HouseTemplate {HouseOne (String name) {super (name);} HouseOne (String name, boolean isBuildToilet) {this (name); this.isBuildToilet = isBuildToilet;} public boolean isBuildToilet; @ Override protected void buildDoor () {System.out.println (name + "doors with security doors");} @ Override protected void buildWindow () {System.out.println (name + "windows facing north") } @ Override protected void buildWall () {System.out.println (name + "the wall is made of marble");} @ Override protected void buildBase () {System.out.println (name + "the foundation is made of steel");} @ Override protected void buildToilet () {System.out.println (name + "the toilet is built in the southeast corner");} @ Override protected boolean isBuildToilet () {return isBuildToilet;}}

Subclass 2:HouseTwo

Public class HouseTwo extends HouseTemplate {HouseTwo (String name) {super (name);} @ Override protected void buildDoor () {System.out.println (name + "doors with wooden doors");} @ Override protected void buildWindow () {System.out.println (name + "windows facing south");} @ Override protected void buildWall () {System.out.println (walls of name + "made of glass") } @ Override protected void buildBase () {System.out.println (name + "the foundation uses granite");} @ Override protected void buildToilet () {System.out.println (name + "the toilet is built in the northwest corner");}}

Test class: Clienter

Public class Clienter {public static void main (String [] args) {HouseTemplate houseOne = new HouseOne ("House 1", false); HouseTemplate houseTwo = new HouseTwo ("House 2"); houseOne.buildHouse (); houseTwo.buildHouse ();}}

Test result

The foundation of house 1 is made of steel foundation, the walls of house 1 are built of marble, the doors of house 1 are built with security doors, the windows of house 1 face north, the foundations of house 2 are made of granite, the walls of house 2 are made of glass, the doors of house 2 are made of wooden doors, the windows of house 2 are built to the south, and the toilets of house 2 are built in the northwest corner.

From the direct results we can clearly see that we customized House 1 by rewriting the hook method without the need to build a toilet (fasle).

The role of the hook method is clear at a glance.

Key points of template mode:

1. Use abstract classes to define template classes, and define all basic methods, template methods, hook methods, unlimited number, mainly to achieve functional logic. The basic method is modified by final, in which the basic method and hook method are called, and the basic method and hook method can be modified by protected, indicating that it can be modified by subclasses.

2. Define subclasses that implement abstract classes, rewrite template methods, or even hook methods, and improve the specific logic.

Use the scene:

1. When you have the same methods in multiple subclasses and the logic is the same, you can extract these methods into a template abstract class.

2. The template method can also be used when the main framework of the program is the same and the details are different.

At this point, the study on "the definition of Java template pattern and application scenario parsing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report