Train Wreck Pattern – A much improved implementation in Java 8
Join the DZone community and get the full member experience.
Join For Free
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.
Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments