Common design patterns - prototype pattern and template pattern

Prototype mode

What is the prototype pattern

Now there is a scenario, that is, many queries or business processing. The parameters accepted by the back end are encapsulated in one object. The processing business calls different processing services. Each processing service may modify the value in the parameter object, so each service needs to pass a new parameter object. At this time, you need to create a new parameter object without calling a service, In order to ensure better performance, it is necessary to quickly create new objects with the same values. The prototype mode is a good choice and belongs to the creation mode.
Simply put, specify the kind of objects to be created, and create new objects by copying these prototypes.


1. Define objects that can be cloned

If a class implements the clonable interface, it can be cloned. Isn't it very simple.

 * After implementing the clonable interface, you can clone the Object using the clone() method of the Object
class ProductWithClone implements Cloneable{

    private String name;
    private int age;
    private String address;

    public ProductWithClone(String name, int age, String address) { = name;
        this.age = age;
        this.address = address;

    public String getName() {
        return name;

    public void setName(String name) { = name;

     * jvm The object attribute is copied
     * @return
     * @throws CloneNotSupportedException
    protected ProductWithClone clone() throws CloneNotSupportedException {
        ProductWithClone product = (ProductWithClone) super.clone();
        product.setName("acai copy");
        return product;

2. Test

public class ProductWithCloneTest {

    public static void main(String[] args) throws CloneNotSupportedException {
        ProductWithClone product = new ProductWithClone("acai", 25, "earth");
        //Clone an object
        ProductWithClone product1 = product.clone();

ProductWithClone{name='acai', age=25, address='earth'}
ProductWithClone{name='acai copy', age=25, address='earth'}
As can be seen from the results, another object was cloned.

Print out the object address to see:
product1 is cloned from the product object.

Template mode

What is the target mode

Define an algorithm skeleton, and implement some specific operations by subclasses, so that subclasses can change the specific implementation steps without changing the algorithm or process. The general process is unchanged and the implementation is changeable. For example, the example of operating a database.
This type of design pattern belongs to behavior pattern.


1. Define an abstract template class

Define a template class, and the operator() method implements the basic business logic, or public business logic.
templateMethod handles different situations.

public abstract class AbstractClass {

    void operator(){
        System.out.println("connect open");
        System.out.println("pre init");
        System.out.println("connect close");
     * Implementation specific process
    abstract void templateMethod();

    abstract void templateMethodV2();

2. Define a subclass to implement the template class

Define a subclass, inherit the template class, and re create the template class to provide methods that can handle specific business logic.

public class SubClass extends AbstractClass{

    void templateMethod() {
        System.out.println("insert into table");

    void templateMethodV2() {
        System.out.println("update other tables");

3. Test

public class TemplateTest {
    public static void main(String[] args) {
        AbstractClass aClass = new SubClass();

connect open
pre init
insert into table
update other tables
connect close
Here, it is assumed that the operation of operating the database. First, connect the database. The preprocessing is completed in the template class. insert into table and update other tables are performed by child operations. connect close is completed in the template class.

Tags: Java Design Pattern

Posted by WebbDawg on Mon, 22 Aug 2022 09:50:24 +0930