Recently in the project development saw a piece of code does not understand, in the method actually called abstract method. Later on the Internet to find that template mode
Public abstract class Jwbaseprocess implements Jwprocess {
Protected jwbaseprocess () {
try{
Workitemmanager = Wapifactory.getworkitemmanager (Jwconstants.flow_task);
} catch (Bpmexception e) {
E.printstacktrace ();
}
}
protected abstract voidPrefetchdata(HttpServletRequest request, Prdonlineprocessform Popform,userinfo user) throws Exception;
public void Prefetchworkitemdata (HttpServletRequest request, Prdonlineprocessform Popform,userinfo user) throws Exception {
Prepare data for page display
Prefetchdata(Request, popform, user);
}
protected abstract void Beforefinishprocess (HttpServletRequest request, prdonlineprocessform popform,userinfo user) Throws Exception;
public void Beforefinishworkitem (HttpServletRequest request, Prdonlineprocessform Popform,userinfo user) throws Exception {
Save the next node execution person
Beforefinishprocess (Request, Popform,user);
}
protected abstract void Afterfinishprocessforward (HttpServletRequest request, Prdonlineprocessform Popform,userinfo User) throws Exception;
public void Afterfinishprocessback (HttpServletRequest request, Prdonlineprocessform Popform,userinfo user) throws exception{
}
public void Afterfinishworkitem (HttpServletRequest request, Prdonlineprocessform Popform,userinfo user) throws Exception {
Save annotations
if (Jivo.getlinkid ()!=null&&jivo.getlinkid (). StartsWith (Prdonlineconstants.prdonline_linkid_back_pre)) {
Afterfinishprocessback (Request, Popform,user);
}else{
Afterfinishprocessforward (Request, Popform,user);
}
}
protected abstract void Savefinishprocess (prdonlineprocessform popform,userinfo user) throws Exception;
public void Savefinishworkitem (prdonlineprocessform popform,userinfo user) throws exception{
Savefinishprocess (Popform,user);
}
Public String checkprocessdata (prdonlineprocessform popform,userinfo user) throws exception{
return prdonlineconstants.prdonline_checkdata_return_value_success;
}
protected void Getprdonlineprdinfo (Prdonlineprdinfovo pivo, Prdonlineacceptprocessvo Accevo, Prdonlineaccountprocessvo Accovo, Prdonlinenoticeprocessvo notivo, Prdonlineparameterprocessvo paraVo, PRDONLINEBILLPROCESSVO Billvo, String code) {
Get information about each process
}
}
Template mode of design pattern
Template Method Pattern: Template method Schema prepares an abstract class, implements partial logic in the form of concrete methods and concrete constructors, and then declares some abstract methods to force subclasses to implement the remaining logic . Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. first, a top-level logical framework is developed, and the details of the logic are left to the specific subclasses to implement . The open and closed principle means that a software entity should be opened to the extension and shut down for modification . This means that the software entity must be expanded without being modified . The template method pattern intention is to control the top-level logic by the abstract parent class, and defer the implementation of the basic operation to the subclass to achieve , this is through the means of inheritance to achieve the reuse of objects, but also adhere to the open and closed principle !
let's explain the diagram in the context of the above definition.
- AbstractClass is equivalent to the above as the parent class of the abstract class,Concreteclass is the specific implementation subclass. In a specific application, there may be one or more implementations of subclasses.
- AbstractClass defines a public templatemethod () template method as well as the method1 ( ) and method2 () methods as steps.
-Abstractclass#templatemethod ()method Body calls method1 () and method2 () methods
Public void Templatemethod () {
...
This.method (1);
...
This.method (2);
...
}
-Method1 ()the method2 () method is a protected abstract method (protectedabstract). implementing Subclass Concreteclass requires overloading the method.
Application Scenarios for Template Method mode :
The Template Method mode is typically used in applications with the following conditions:
- have a uniform operating procedure or operating procedure
- with different operating details
- There are multiple scenarios with the same steps, but some specific operational details are different
We should be able to find out what the same parts and different parts of the realization of one thing are:
For example: production of mobile phones: the production process is the same, but the specific details may vary ( such as the color of the phone, cell phone category, cell phone size, etc. );
Let's use the code to implement this small example:
The first step is to create an abstract template class:
Package Templatedemo;
/**
* This is an abstract template class
* */
Abstract class AbstractClass {
Define a template approach to control the flow of production phones
Public void makemobile () {//templatemethod
First production of mobile phone body (template method to complete its own)
System. out. println ("produced a cell phone body");
this. makedifferentfunction ();
this. Makedifferentcolor ();
this. Makedifferenttype ();
System. out. println ("A mobile phone production completed");
The above template has been stipulated.
}
Define abstract methods to complete different production details (phones with different functions)
protected Abstract void makedifferentfunction ();
Define abstract methods to complete different production details (phones of different colors)
protected Abstract void makedifferentcolor ();
Define abstract methods to complete different production details (different types of phones)
protected Abstract void makedifferenttype ();
}
/**
* Create real implementation classes to achieve specific production details
* */
class Concreteclass extends abstractclass{
Methods to implement abstractions in the parent class
protected void Makedifferentcolor () {
System. out. println ("Paint the color of the phone blue");
}
protected void makedifferentfunction () {
System. out. println ("for the mobile phone to see the function of MP4");
}
protected void Makedifferenttype () {
System. out. println ("Make a clamshell on the phone");
}
}
Write Test class
Public class testtemplate{
Public Static void Main (String args[]) {
AbstractClass AC = new concreteclass ();//The object of the subclass is paid to the parent class
Ac.makemobile ();//Make Phone
Make a different phone just modify the subclass to do it.
}
}
If you want to complete different implementation details, just write a different subclass, the template has already stipulated the implementation of the process !
This example of a simple template method pattern is done!
Template mode for Java design patterns