Train Wreck Pattern – A much improved implementation in Java 8

Venkat Subramaniam at a talk today mentioned about Cascade Method pattern or Train Wreck pattern which looks something like: someObject.method1().method2().method3().finalResult()

Few might associate this with the builder pattern, but its not the same. Anyways lets have a look at an example for this in Java with out the use of lambda expression:

public class TrainWreckPattern {
  public static void main(String[] args) {
    new Mailer()
    .to("to@example.com")
    .from("from@exmaple.com")
    .subject("Some subject")
    .body("Some content")
    .send();
    
  }
}

class Mailer{
  public Mailer to(String address){ 
    System.out.println("To: "+address); 
    return this;
  }
  public Mailer from(String address){ 
    System.out.println("From: "+address); 
    return this;
  }
  public Mailer subject(String sub){ 
    System.out.println("Subject: "+sub); 
    return this;
  }
  public Mailer body(String body){ 
    System.out.println("Body: "+body); 
    return this;
  }
  public void send(){ 
    System.out.println("Sending ..."); 
  }
}

I have taken the same example which Venkat Subramaniam took in his talk. In the above code I have a Mailer class which accepts a series of values namely: to, from, subject and a body and then sends the mail. Pretty simple right? But there is some problem associated with this: One doesn’t know what to do with the Mailer object once it has finished sending the mail. Can it be reused to send another mail? Or should it be held to know the status of email sent? This is not known from the code above and lot of times one cannot find this information in the documentation. What if we can restrict the scope of the Mailer object within some block so that one cannot use it once its finished its operation?

Java 8 provides an excellent mechanism to achieve this using Lambda expressions. lets look at how it can be done:

public class TrainWreckPatternLambda {

  public static void main(String[] args) {
    Mailer.send( mailer -> {
      mailer.to("to@example.com")
            .from("from@exmaple.com")
            .subject("Some subject")
            .body("Some content");
    });
  }

}

class Mailer{

  private Mailer(){
    
  }
  public Mailer to(String address){ 
    System.out.println("To: "+address); 
    return this;
  }
  public Mailer from(String address){ 
    System.out.println("From: "+address); 
    return this;
  }
  public Mailer subject(String sub){ 
    System.out.println("Subject: "+sub); 
    return this;
  }
  public Mailer body(String body){ 
    System.out.println("Body: "+body); 
    return this;
  }
  public static void send(Consumer<Mailer> mailerOperator){ 
    Mailer mailer = new Mailer();
    mailerOperator.accept(mailer);
    System.out.println("Sending ..."); 
  }
}

In the above implementation I have restricted the instantiation of the Mailer class to the send() method by making the constructor private. And then the send() method now accepts an implementation of Consumer interface which is a Single Abstract method class and can be represented by a Lambda expression. And in the main() method I pass a lambda expression which accepts a Mailer instance and then configures the mailer object before being used in the send() method.

The use of lambda expression has created a clear boundary for the use of the Mailer object and this way its much ore clearer for someone reading the code as to how the Mailer object has to be used.

Let me know if there is something more that I can improve in this example I have shared.

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading