Java and design patterns-template patterns

Source: Internet
Author: User

Java and design patterns-template patterns

The template mode can be understood as the step mode, and the steps can be clearly followed when you add the template mode. The concept of a template should be very familiar to many people. Write a resume and a thesis. If there is a template for our reference, it will be easier to write and more handy. This resume template and thesis template can be considered as specifying the steps for writing resumes and papers. We only need to follow this step to write them step by step.

The UML class diagram of the template mode is as follows:

Here we will explain how to use the template mode through an example in our daily life. For beginners, cooking is based on a step-by-step process, which can be roughly divided into the following steps: Fire-add Edible Oil-add food-Add seasoning-Attach disk. Use the code to implement one by one. First, create an abstract class as a template class:

AbstractCookTemplate. java (corresponding to AbstractClass in the class diagram ):

Package com. template. demo; public abstract class implements actcooktemplate {public final void cook () {System. out. println ("-------- start cooking -----------"); fire (); // fire oil (); // pour oil into chaocai (); // stir-fry zuoliao (); // Add zhuangpan (); // attach the System disk. out. println ("-------- end cooking -----------");} protected void zhuangpan () {// sets the permission protected, so that the subclass can override System. out. println ("disk");} protected void zuoliao () {System. out. println ("Add seasoning");} protected void chaocai () {System. out. println ("sauteed");} protected void oil () {System. out. println ("add oil");} protected void fire () {System. out. println ("fire ");}}

It is difficult to write some English words. Given that the English level of the landlord is limited, it is replaced by pinyin. In actual development, this method is not available.

Implementation class-CookChicken. java (corresponding to ConcreteClassA ):

Package com. template. demo; public class CookChicken extends AbstractCookTemplate {@ Overrideprotected void chaocai () {System. out. println ("fried chicken ");}}

Implementation class-CookFish. java (corresponding to ConcreteClassB ):

Package com. template. demo; public class CookFish extends AbstractCookTemplate {@ Overrideprotected void zhuangpan () {System. out. println (" ");}}

Test class:

package com.template.demo;public class TestClass {public static void main(String[] args) {AbstractCookTemplate abstractCookTemplate=new CookChicken();abstractCookTemplate.cook();AbstractCookTemplate abstractCookTemplate2=new CookFish();abstractCookTemplate2.cook();}}


Run the test class:

The example is far-fetched. Well, you can understand it. Some people have said that what should I do if I want to mix a cucumber and don't need to get angry?

In this case, we can use the Hook method. For details about the hooks, refer to the Code:

AbstractCookTemplate. java (corresponding to AbstractClass in the class diagram ):

Package com. template. demo; public abstract class implements actcooktemplate {public final void cook () {System. out. println ("-------- start cooking -----------"); if (! IsColdFood () {fire (); // fire} oil (); // pour oil into chaocai (); // sauteed food zuoliao (); // Add zhuangpan (); // mount the System. out. println ("-------- end cooking -----------");} protected boolean isColdFood () {// return false as the hook;} protected void zhuangpan () {// set the permission to protected, this facilitates subclass overwriting of System. out. println ("disk");} protected void zuoliao () {System. out. println ("Add seasoning");} protected void chaocai () {System. out. println ("sauteed");} protected void oil () {System. out. println ("add oil");} protected void fire () {System. out. println ("fire ");}}


We can see that before the fire is realized, we can determine whether the method of fire is allowed if it is not cool. The default value is fire.

If the fire generation method is not required, override the isColdFood () method in the implementation class and return true, that is, the fire () method is not run.

At this time, we implement an implementation class. The following is the salad cucumber:

package com.template.demo;public class CookCucumber extends AbstractCookTemplate {@Overrideprotected boolean isColdFood() {return true;}}

Add the following implementation to the test class:

package com.template.demo;public class TestClass {public static void main(String[] args) {AbstractCookTemplate abstractCookTemplate=new CookChicken();abstractCookTemplate.cook();AbstractCookTemplate abstractCookTemplate2=new CookFish();abstractCookTemplate2.cook();AbstractCookTemplate abstractCookTemplate3=new CookCucumber();abstractCookTemplate3.cook();}}


Then run the instance:

At this time, we can see that the last life method is not running. Similarly, we can use the Hook method to determine whether other methods are running. In this way, our templates will be more flexible and meet various needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.