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

Behavioral pattern: template method

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

Share

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

The article begins with:

Behavioral pattern: template method

One of the eleven behavioral models: template method.

Brief introduction

Name: template method

English name: Template Method Pattern

Values: under my control, let you play

Personal introduction:

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.

Define a framework for the algorithm in the operation, and defer some steps to subclasses. So that subclasses can redefine some specific steps of an algorithm without changing the structure of the algorithm.

(from the Zen of Design patterns)

Explain the above introduction, which means that the framework is defined by the parent class and implemented by the subclass.

The story you want.

Did everyone buy new shoes just after the Spring Festival? Today's story is about shoes. From the surface, a pair of shoes is composed of soles, insoles, uppers and laces. These parts of the same series of shoes are all the same. If they are made of the same material, different series of shoes will be very different. According to the template method model, the manufacturing process of assembling a pair of shoes can be merged into a fixed framework, and as for what materials are used, it is realized by each series of shoes. Let's first look at the frame code that defines the assembly of shoes.

/ * define the process framework for shoe manufacturing * / abstract class ShoeInstallTemplate {public abstract void installSole (); public abstract void installInsole (); public abstract void installVamp (); public abstract void installShoelace (); public void installShot () {System.out.println ("assemble a pair of shoes, the steps are as follows:"); / / assemble sole installSole (); / / assemble insole installInsole () / / assemble upper installVamp (); / / assemble shoelace installShoelace ();}}

An abstract ShoeInstallTemplate for assembling a shoe framework is defined, in which there are four processes that are not specifically implemented and are implemented by the shoe manufacturer, because only the shoe manufacturer knows what materials the shoes are made of.

Here are two famous shoes: Adidas's Boost series and Nike's Jordan series. The following is to realize the manufacturing code of these two series of shoes respectively.

/ * AdidasBoost shoes Manufacturing * / class AdidasBoostShoeInstall extends ShoeInstallTemplate {@ Override public void installSole () {System.out.println ("assemble white Boost soles");} @ Override public void installInsole () {System.out.println ("assemble black Boost insoles");} @ Override public void installVamp () {System.out.println ("assemble black Boost uppers") } @ Override public void installShoelace () {System.out.println ("assemble black Boost laces");}} / * NikeJordan shoes manufacture * / class NikeJordanShoeInstall extends ShoeInstallTemplate {@ Override public void installSole () {System.out.println ("assemble black Jordan soles");} @ Override public void installInsole () {System.out.println ("assemble black Jordan insoles") } @ Override public void installVamp () {System.out.println ("assemble red Jordan uppers");} @ Override public void installShoelace () {System.out.println ("assemble red Jordan laces");}}

After implementing the code for manufacturers to make shoes, we tested how to make Boost and Jordan shoes through the code.

Public class TemplateMethodTest {public static void main (String [] args) {ShoeInstallTemplate adidasBoost = new AdidasBoostShoeInstall (); adidasBoost.installShot (); ShoeInstallTemplate nikeJordan = new NikeJordanShoeInstall (); nikeJordan.installShot () }} print result: assemble a pair of shoes, the steps are as follows: assembling white Boost soles, assembling black Boost insoles, assembling black Boost uppers, assembling black Boost laces, assembling a pair of shoes, the steps are as follows: assembling black Jordan soles, assembling black Jordan insoles, assembling red Jordan uppers, assembling red Jordan laces.

The template method pattern is as simple as that. Do you have it?

Code:

Template Method Pattern

Summary

Template method is a more practical model, why is it practical? To take a realistic example, Java can not develop without major open source frameworks, such as Dubbo. Friends who have seen the source code will know that a lot of code uses template design patterns. Why can Dubbo support many kinds of registries? In fact, the essence is to use the template method to design patterns, so that a variety of registries can be extended. Mastering the template method is of great help to read the source code. Many people, including me, doubted life for quite a long time when they first started reading the source code. Why do these codes go around so much? Transferred back and forth. When you understand the commonly used design patterns, you can directly know what design pattern is used by looking at the source code, and why use this design pattern? It was for something. With this layer of thinking, it is like a line that connects knowledge points scattered around the country and becomes knowledge that can be deliberated.

Recommended reading:

Single responsibility principle (method: change the name or password? Interface: washing dishes, buying vegetables or taking out the trash? Classes: register, log in, and log out)

Richter substitution principle (my son is from New Oriental Cooking)

Rely on the principle of inversion (a stingy restaurant owner)

Interface isolation principle (young man's workshop)

Dimitt's rule (reading e-books on mobile phones)

The principle of opening and closing (social security)

Creative mode: singleton mode (Xiaoming has only one car)

Creative model: factory method (Xiaoming's garage)

Creative model: abstract factory (BMW cars have to use BMW tires and BMW steering wheels)

Creative model: builder mode (soup is so hot)

Creative model: prototype model (photocopying books)

The official account replied "Gift package" to get tutorials such as Java, Python, IOS, etc.

Add personal Wechat comments "tutorials" to get tutorials for architects, machine learning, etc.

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