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
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You'll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You'll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


six + = 15



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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