• 周二. 12 月 31st, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

【java_ The wonderful use of template method design pattern: the implementation of callback subclass

King Wang

1 月 3, 2022

Template method design pattern application scenarios

HttpServlet Of service() It’s a template method , It has achieved Servlet standard ,
DispatcherServlet With the help of service() Template method , Expand their own logic .
HttpServlet Is an abstract class . Abstract classes are the cornerstone of implementing template methods , Here’s a simple example , And then prove it from the bottom

example

Product line definition :

/**
* @Author james
* @Description Shoe product line , abstract class . This class implements the specification of the product
* @Date 2019/12/23
*/
public abstract class ShoesProduct implements Product{

// The default is not online 
private boolean available = false;
public boolean isAvailable() {

return available;
}
public void setAvailable(boolean available) {

this.available = available;
}
/**
* The default implementation is left to subclasses , Unrealized , The function is not supported , Call and throw exception
*
*/
protected void payOnline() {

throw new RuntimeException(" The product does not have online payment function ");
}
protected void sevenDay(){

throw new RuntimeException(" This product does not support refund for 7 days without reason ");
}
/**
* Template method for shoe product line to generate orders
*
*/
@Override
public void makeOrder() {

if (isAvailable()) {

payOnline();
sevenDay();
} else {

throw new RuntimeException(" The product is not online , Unable to generate order ");
}
}
}

The specific product line realizes

/**
* @Author james
* @Description The concrete realization of product line :Nike Series product line
* @Date 2019/12/23
*/
public class NikeShoesProduct extends ShoesProduct {

// Product name 
private String name;
// Is it a joint name 
private boolean isJoint = false;
@Override
protected void payOnline() {

System.out.println(" Support Alipay payment ");
}
/**
* Joint sale , Self expanding business functions
*/
private void jointOffer() {

System.out.println(name + " A joint Bape sale ");
}
/**
* The realization of specific product generated orders
*/
@Override
public void makeOrder() {

if (isJoint()) {

jointOffer();
super.makeOrder();
} else {

super.makeOrder();
}
}
/**
* Determine if the product is online , Name the product
*
* @param isAvailable Is it online
*/
public NikeShoesProduct(boolean isAvailable, String name) {

this.name = name;
super.setAvailable(isAvailable);
}
public NikeShoesProduct(boolean isAvailable, boolean isJoint, String name) {

this.name = name;
this.isJoint = isJoint;
super.setAvailable(isAvailable);
}
public String getName() {

return name;
}
public void setName(String name) {

this.name = name;
}
public boolean isJoint() {

return isJoint;
}
public void setJoint(boolean joint) {

isJoint = joint;
}
}

Business scenario approach

 // nikeID 
ShoesProduct nikeShoesProduct = new NikeShoesProduct(true, true, "nike ID Winter series ");
nikeShoesProduct .makeOrder(); // Generate order information for the product 

 Insert picture description here
Achieve the desired goal . The expectation here refers to : Abstract class as parent class , You can call back to the implementation of the subclass through abstract methods , It’s a fantastic process . You can observe the following call chain

  1. Subclass instances of abstract classes nikeShoesProduct -> Call the makeOrder();
  2. nikeShoesProduct .makeOrder() -> perform super.makeOrder() -> Call the abstract class ShoesProduct.makeOrder()
    The implementation of the following abstract classes :
    /**
    * The default implementation is left to subclasses , Unrealized , The function is not supported , Call and throw exception
    *
    */
    protected void payOnline() {
    
    throw new RuntimeException(" The product does not have online payment function ");
    }
    protected void sevenDay(){
    
    throw new RuntimeException(" This product does not support refund for 7 days without reason ");
    }
    /**
    * Generate order
    *
    */
    @Override
    public void makeOrder(){
    
    if (isAvailable()) {
    
    payOnline();
    sevenDay();
    } else {
    
    throw new RuntimeException(" The product is not online , Unable to generate order ");
    }
    }
    
  3. The abstract class makeOrder()-> call payOnline() , This is the key point of this paper
    payOnline() It’s called after all The abstract class payOnline() Or implement the class payOnline()
    The answer is : Implementation class payOnline()

analysis

This is supposed to be a very basic question , Before writing code, I always stayed at the level of looking at the code , I remember this For this class ,super Represents the parent class , It’s not accurate enough , Only in debug I just exposed my problems .
In fact, on a call chain ,this The object that represents the start of the call chain , Implementation class through super Access the method of the parent class , Runtime , Of the parent class this The implementation class

 Insert picture description here
 Insert picture description here

extend

The template method realizes the scene where the parent class calls the child class , And why Take the inheritance abstract class instead of directly inheriting the common class

  1. Abstract class cannot be instantiated , this The object of is directly identified as the implementation class , Business provides a single entry
    1.1 It is worth mentioning that , In an abstract class makeOrder() In the payOnline() It will also become this.payOnlin
  2. Abstract classes also participate in the adapter pattern ,abstract class HttpServlet extends GenericServlet Adapted GenericServlet, Also let Servlet Good support Http agreement , DispatcherServlet With the help of our efforts, we have realized Spring Core controller of .Spring A more typical example of this would be Adapter pattern 、spring relevant

发表回复