DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Implementation Best Practices: Microservice API With Spring Boot
  • Exploring Throttling in Java: Simple Implementation Examples - Part 1
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • A Keycloak Example: Building My First MCP Server Tools With Quarkus
  • Beyond the Glass Slab: How AI Voice Assistants are Morphing Into Our Real-Life JARVIS
  • MCP Client Agent: Architecture and Implementation
  • Rust: The Must-Adopt Language for Modern Software Development
  1. DZone
  2. Coding
  3. Java
  4. Train Wreck Pattern – A much improved implementation in Java 8

Train Wreck Pattern – A much improved implementation in Java 8

By 
Mohamed Sanaulla user avatar
Mohamed Sanaulla
·
May. 15, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
11.3K Views

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("[email protected]")
    .from("[email protected]")
    .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("[email protected]")
            .from("[email protected]")
            .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.

Implementation Java (programming language)

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Implementation Best Practices: Microservice API With Spring Boot
  • Exploring Throttling in Java: Simple Implementation Examples - Part 1
  • Effective Java Collection Framework: Best Practices and Tips

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: