Do you know the template method pattern of the default method of Lambda expression in Java 8?

In order to describe the template method in simpler terms, consider this scenario: Suppose in a workflow system, in order to complete tasks, there are 4 tasks that must be executed in a given execution order. In these four tasks, the implementation of different workflow systems can customize the execution content of the task according to its own situation.

The template method can be applied to the above scenarios: encapsulate the four core tasks of the workflow system into abstract classes, and if the tasks can be customized, postpone the customizable tasks to subclasses.

Code:

/** 
 * Abstract Workflow system 
 */
abstract class WorkflowManager2{

    public void doTask1(){

        System.out.println("Doing Task1...");

    }

    public abstract void doTask2();

    public abstract void doTask3();

    public void doTask4(){

        System.out.println("Doing Task4...");

    }

}

/** 
 * One of the extensions of the abstract workflow system 
 */
class WorkflowManager2Impl1 extends WorkflowManager2{

    @Override
    public void doTask2(){

        System.out.println("Doing Task2.1...");

    }

    @Override
    public void doTask3(){

        System.out.println("Doing Task3.1...");

    }

}

/** 
 * Other extension of the abstract workflow system 
 */
class WorkflowManager2Impl2 extends WorkflowManager2{

    @Override
    public void doTask2(){

        System.out.println("Doing Task2.2...");

    }

    @Override
    public void doTask3(){

        System.out.println("Doing Task3.2...");

    }

}

Let's take a look at how the workflow system is used:

public class TemplateMethodPattern {

    public static void main(String[] args) {

        initiateWorkFlow(new WorkflowManager2Impl1());

        initiateWorkFlow(new WorkflowManager2Impl2());

    }

    static void initiateWorkFlow(WorkflowManager2 workflowMgr){

        System.out.println("Starting the workflow ... the old way");

        workflowMgr.doTask1();

        workflowMgr.doTask2();

        workflowMgr.doTask3();

        workflowMgr.doTask4();

    }

}

The output looks like this:

Starting the workflow ... the old way

Doing Task1...

Doing Task2.1...

Doing Task3.1...

Doing Task4...

Starting the workflow ... the old way

Doing Task1...

Doing Task2.2...

Doing Task3.2...

Doing Task4...

So far everything goes well. But the main focus of this blog is not the template method pattern, but how to implement the template method pattern using Java 8 Lambda expressions and default methods. I have said before that Lambda expressions can only be used in an interface if only one abstract method is declared. This rule should be explained in the example in this article: WorkflowManager2 can only have one abstract or custom task.

We can replace abstract classes with interfaces with default methods, so our new workflow system looks like this:

interface WorkflowManager{

    public default void doTask1(){

        System.out.println("Doing Task1...");

    }

    public void doTask2();

    public default void doTask3(){

        System.out.println("Doing Task3...");

    }

    public default void doTask4(){

        System.out.println("Doing Task4...");

    }

}

Now our workflow system has a customizable task 2. Let's go down and use Lambda expressions to handle the initialization work:

public class TemplateMethodPatternLambda {

    public static void main(String[] args) {

       /**     
        * Using lambda expression to create different      
        * implementation of the abstract workflow 
        */
        initiateWorkFlow(()->System.out.println("Doing Task2.1..."));

        initiateWorkFlow(()->System.out.println("Doing Task2.2..."));

        initiateWorkFlow(()->System.out.println("Doing Task2.3..."));

    }

    static void initiateWorkFlow(WorkflowManager workflowMgr){

        System.out.println("Starting the workflow ...");

        workflowMgr.doTask1();

        workflowMgr.doTask2();

        workflowMgr.doTask3();

        workflowMgr.doTask4();

    }

}

This is an example of Lambda expressions applied to the template method pattern. '

At last

If you want to learn Java in the West Hall, receive the Java interview question summary of the first-line manufacturers + Alibaba Taishan manual + learning thinking guide for each knowledge point + a summary of Java core knowledge points in a 300-page pdf document!

The content of these materials are all the knowledge points that the interviewer must ask during the interview. The chapter includes many knowledge points, including basic knowledge, Java collections, JVM, multi-threaded concurrency, spring principles, microservices, Netty and RPC, Kafka , Diary, design pattern, Java algorithm, database, Zookeeper, distributed cache, data structure, etc. file

Guess you like

Origin blog.csdn.net/weixin_46577306/article/details/107965028