Design mode 8---template mode

Source: Internet
Author: User

Question: There are a number of students need to answer questions, if using a handwritten way, the topic is easy to make mistakes, and if the copy, we have the same title.

Industry is to say: for students, in addition to the answer is inconsistent, others should be consistent.

Industry is, as far as possible, the same code in the form of a parent class package,

Other differences can be presented in more than one new style!

 Public Abstract classAbstracttemplate {/*** Template Method*/     Public voidTemplatemethod () {//calling the basic methodAbstractmethod ();        Hookmethod ();    Concretemethod (); }    /*** Declaration of basic methods (implemented by subclasses)*/    protected Abstract voidAbstractmethod (); /*** Basic method (empty method)*/    protected voidHookmethod () {}/*** Basic method (already implemented)*/    Private Final voidConcretemethod () {//Business-related code    }}
 Public classConcretetemplateextendsabstracttemplate{//the implementation of the basic method@Override Public voidAbstractmethod () {//Business-related code    }    //overriding methods of the parent class@Override Public voidHookmethod () {//Business-related code    }}
Default Hook method

A hook method is often given by an abstract class to an empty implementation as the default implementation of this method. This empty hook method is called "Do nothing Hook". Obviously, this default hook method has already been seen in the default adaptation mode, and a default adaptation mode is that a class provides a default null implementation for an interface, so that the subclass of the default adaptation class does not have to give the implementation of all the methods as the implementation of the interface, because usually a concrete class does not need all the methods.

Naming rules

Naming rules are one of the channels by which designers communicate, and using proper naming conventions can help communicate between different designers.

The name of the hook method should begin with do, which is a standard practice for Java developers who are familiar with design patterns. In the above example, the hook method Hookmethod () should start with do, and in the HttpServlet class, this naming convention, such as Doget (), DoPost (), is also followed.

Design mode 8---template mode

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.