Train Wreck Pattern / Cascade Method Pattern In C#

Real World Scenario

Consider a scenario where you have to create an application, having recharge and utility bill payment functionality.

utility

In order to create an application, you have to create Recharge class which accepts a series of values like Service, Operator, Circle, Mobile and Amount and then called the DoRecharge method. But there is a problem associated with this approach. One doesn’t know what to do with the Recharge object once it has finished doing the recharge. Can it be reused to do another recharge or should it be held to know the status of recharge transaction?

  1. namespace TrainWreckPattern  
  2. {  
  3.     public class Recharge   
  4.     {  
  5.         public string Service  
  6.         {  
  7.             get;  
  8.             set;  
  9.         }  
  10.         public string Operator {  
  11.             get;  
  12.             set;  
  13.         }  
  14.         public string Circle {  
  15.             get;  
  16.             set;  
  17.         }  
  18.         public string Mobile {  
  19.             get;  
  20.             set;  
  21.         }  
  22.         public double Amount {  
  23.             get;  
  24.             set;  
  25.         }  
  26.         public void DoRecharge() {  
  27.             // Do Recharge  
  28.         }  
  29.     }  
  30. }  
  31. namespace TrainWreckPattern  
  32. {  
  33.     class Program  
  34.     {  
  35.         static void Main(string[] args)   
  36.         {  
  37.             Recharge objRecharge = newRecharge();  
  38.             objRecharge.Service = "Topup";  
  39.             objRecharge.Operator = "Vodafone";  
  40.             objRecharge.Circle = "Gujarat";  
  41.             objRecharge.Mobile = "9769496026";  
  42.             objRecharge.Amount = 100;  
  43.             objRecharge.DoRecharge();  
  44.         }  
  45.     }  
  46. }  
To figure out the above troubles, we are going to use cascade or train wreck pattern. This improves readability of a code because it avoids repeated references to the instance. So, we can call multiple methods to be called on the same object. This concept was first introduced in Smalltalk programming language.

Here, I have made couple of changes in the code. I have removed all properties, created a couple of methods like Service, Operator etc. and each one returns instance of class itself except DoRecharge() method.
  1. namespace TrainWreckPattern  
  2. {  
  3.     public class Recharge  
  4.     {  
  5.         public Recharge Service(string service)  
  6.         {  
  7.             return this;  
  8.         }  
  9.         public Recharge Operator(string operators) {  
  10.             return this;  
  11.         }  
  12.         public Recharge Circle(string circle) {  
  13.             return this;  
  14.         }  
  15.         public Recharge Mobile(string mobile) {  
  16.             returnthis;  
  17.         }  
  18.         public Recharge Amount(double amount) {  
  19.             returnthis;  
  20.         }  
  21.         public void DoRecharge() {  
  22.             // Do Recharge  
  23.         }  
  24.     }  
  25. }  
Now we can call DoRecharge method in the following way:
  1. namespace TrainWreckPattern  
  2. {  
  3.     clas sProgram  
  4.     {  
  5.         static void Main(string[] args)   
  6.         {  
  7.             new Recharge().Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100).DoRecharge();  
  8.         }  
  9.     }  
  10. }  
If you look at the above code closely, the beauty of the code is that individual method returns this, so we can invoke each method without making a new line. This is the correct way to use Cascade pattern or method cascading. This pattern is a very useful if object has a very lengthy expression value because it removes the need of using temporary variable or need to either list the expression repeatedly

So, using such pattern we can remove verbosity from code. Now, after looking at the above code, some of you might be thinking that we have been writing such code a million times particularly when we are writing LINQ query.
  1. string[] operators =  
  2.   {  
  3.     "Airtel",  
  4.     "Idea",  
  5.     "Vodafone",  
  6.     "Uninor"  
  7. };  
  8. IEnumerable < string > query = from x in operators  
  9. where x.Contains("a"// Filter Elements  
  10. orderby x.Length // Sort Elements  
  11. select x.ToUpper(); // Project Each Element  
That’s absolutely right. This pattern particularly applied in fluent interfaces. An interface is said to be fluent if it returns itself from one or more its method. This allow users to chain calls together. A fluent interface is an implementation of an object oriented API that aims to provide more readable code. All the LINQ queries including above and others use this concept.

We can also achieve the same thing using lambda:
  1. public static void DoRecharge(Action < Recharge > action)  
  2. {  
  3.     Recharge recharge = newRecharge();  
  4.     action(recharge);  
  5.     // Do Recharge  
  6. }  
  7. Recharge.DoRecharge((recharge) =>  
  8. {  
  9.     recharge.Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100);  
  10. });  
So fundamentally, cascade or train wreck pattern is just syntactic sugar which makes our code more fluid and understandable for reading.
 
Read more articles on C#:

 

Up Next
    Ebook Download
    View all
    Learn
    View all