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