Java Design Pattern template method pattern, java Design Pattern Template

Source: Internet
Author: User

Java Design Pattern template method pattern, java Design Pattern Template
Template Method Mode

Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.In layman's terms, there are many identical steps. Some differences may apply to this mode in some places, such as the test scenario mentioned in the big talk design model, each person has the same exam, but the answer is different. This scenario is suitable for the template method mode. This time I wrote about the AUTO START process. Each AUTO START process is basically the same, but there are some minor differences in this process.

Template Method mode UML diagram

  

Template Method mode code
Package com. roc. template;/*** automobile model * model mode * @ author liaowp **/public abstract class CarModel {/*** automobile start */protected abstract void start (); /*** stop */protected abstract void stop ();/*** the user does not need to pay attention to how your car starts or stops, you can stop it. */final public void excet () {this. start (); this. stop ();}}
Package com. roc. template;/*** * @ author liaowp **/public class Wcar extends CarModel {@ Override protected void start () {System. out. println ("Volkswagen starting ........ Abrupt ") ;}@ Override protected void stop () {System. out. println (" parking lot ......... ");}}
Package com. roc. template; public class Ocar extends CarModel {@ Override protected void start () {System. out. println ("Audi keyless startup burst");} @ Override protected void stop () {System. out. println ("Audi parking ");}}
Package com. roc. template;/*** Client * @ author liaowp **/public class Client {public static void main (String [] args) {CarModel wcar = new Wcar (); // The first car in the House. As a user, we do not need to pay attention to how the car starts. the subclass variable becomes the parent class. Polymorphism wcar. excet (); // suddenly the house needs a second car. We don't need to pay attention to how he produces and starts the CarModel ocar = new Ocar (); ocar. excet ();}}

Template Method mode applicable scenarios

  • Implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.
  • Common behaviors in each subclass should be extracted and concentrated into a common parent class to avoid code duplication. This is a good example of how Opdyke and Johnson describe "repartitioning to generalization. First, identify the differences in the existing code and separate the differences into new operations. Finally, replace these different codes with a template method that calls these new operations.
  • Control subclass extension. The template method only calls the "hook" operation at specific points, so that the extension can only be performed at these points.

 

Related Article

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.