Template Method Pattern – Using Lambda Expressions, Default Methods

Template Method pattern is one of the 23 design patterns explained in the famous Design Patterns book by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. The intent of this pattern is stated as:

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. TemplateMethod lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

To explain in simple terms, consider the following scenario: Assume there is a workflow system in which 4 tasks have to be performed in the given order so as to successfully complete the workflow. Some of the tasks out of the 4 tasks can be customised by
 
different workflow system implementation.

Template Method pattern can be applied to above scenario by encapsulating the workflow system into an abstract class with few of the tasks out of the 4 tasks implemented. And leave the implementation of remaining tasks to the subclasses of the abstract class.

So the above when implemented:

/**
 * 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 me just go ahead and show how these workflow implementations are 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();
  }
}

and the output would be..

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 so good. But the main intent of this post is not to create yet another blog post on Template Method pattern, but to see how we can leverage Java 8 Lambda Expression and Default Methods. I have already written before, that only interfaces which have Single Abstract Methods can be written as lambda expressions. What this translates to in this example is that, the WorkflowManager2 can only have one abstract/customizable task out of the 4 tasks.

So restricting to one abstract method is a major restriction and may not be applicable in many realtime scenarios. I dont wish to reiterate the same old Template Method pattern examples, instead my main intention of writing this is to show how lambda expressions and default methods can be leveraged in scenarios where you are dealing with abstract classes with single abstract methods.

If you are left wondering what these lambda expressions in java mean and also these default methods in java, then please spend some time to read about lambda expressions and default methods before proceeding further.

Instead of an abstract class we would use an interface with default methods, so our workflow system would look like:

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 that we have the workflow system with customisable Task2, we will go ahead and initiate some customised workflows using Lambda expressions…

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 in a small way lambda expressions can be leveraged in the Template Method Pattern
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× 8 = eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close