J2EE (4) servlet template method mode

Source: Internet
Author: User
  1. Many design patterns are used in Java. Our commonly used Servlet classes are implemented using a template method. When processing client requests, you do not have to worry about calling post or GET requests, this class will be judged by ourselves. We need to implement the specific implementation.
  2. It is applied to such a scenario. When we want to complete a process or a series of steps with the same level of details, the implementation of individual steps at a more detailed level may be different, we usually consider using the template method mode for processing.
    1. For example, in the implementation of Servlet classes, there are many methods such as dopost ()/doget (). The call sequence between them has been defined, the specific implementation is implemented by its subclass. Because each request is different, but they call the same method, different implementations are completed by sub-classes.
  1. Java code

    1. Package COM. bjpowernode. pattern;/*** abstract class, template * @ author lls **/public abstract class abstractclass {public static final string method_1 = "M1 "; public static final string method_2 = "m2"; // template method public final void templatemethod (string methodname) {If (method_1.equals (methodname) {dooperater1 (methodname );} if (method_2.equals (methodname) {dooperater2 (methodname) ;}// method 1 protected abstract void dooperater1 (string methodname ); // method 2 protected abstract void dooperater2 (string methodname );}
    2. Package com. bjpowernode. pattern;/*** specific implementation class * @ author lls **/public class concreteclass extends abstractclass {

      @ Override protected void dooperater1 (string methodname) {system. Out. println ("concrete -- operater1" + methodname );}

      @ Override protected void dooperater2 (string methodname) {system. Out. println ("concrete -- operater1" + methodname );}

      }

    3.  

      Package com. bjpowernode. pattern;

      Public class test {

      /*** Test template method ** @ Param ARGs */public static void main (string [] ARGs) {abstractclass A = new concreteclass ();. templatemethod (abstractclass. method_1 );}

      }

       

  1. The template method mode shows its advantages by moving the unchanged behavior to the superclass and removing repeated code from the subclass. It provides a good platform for getting code, because sometimes we encounter a process consisting of a series of steps that needs to be executed, which is the same at a high level, however, the implementation of some steps may be different.
  1. When immutable and mutable behaviors are mixed in the implementation of the method subclass, immutable behaviors are repeated in the subclass, we use the template method to move these actions to a single place, which helps the subclass to get rid of the entanglement of repeated unchanged behaviors.

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.