public
anonymous / gist:416324
Last active

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
public class Program
{
public static void Main()
{
var reader1 = new FileFormatReader1();
var reader2 = new FileFormatReader2();
reader1.Successor = reader2;
 
//The building of the above could be done automatically from an ordered list of FileFormatReaders
string messageBody = "";
reader1.SetMessageBody("some file contents", ref messageBody);
}
}
 
public abstract class FileFormatReader {
public abstract void SetMessageBody(string fileContents, ref string body);
public FileFormatReader Successor { get; set; }
protected void ContinueWithSuccessor(string fileContents, ref string body)
{
if(Successor != null)
Successor.SetMessageBody(fileContents, ref body);
}
}
 
public class FileFormatReader1 : FileFormatReader
{
public override void SetMessageBody(string fileContents, ref string body)
{
if(false)
{
body = "messageBody";
}
else
{
ContinueWithSuccessor(fileContents, ref body);
}
}
}
 
public class FileFormatReader2 : FileFormatReader
{
public override void SetMessageBody(string fileContents, ref string body)
{
if(true)
{
body = "messageBody";
}
else
{
ContinueWithSuccessor(fileContents, ref body);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.