Skip to content

Instantly share code, notes, and snippets.

@Kritner
Last active May 31, 2017 18:48
Show Gist options
  • Save Kritner/a4165cbdb686364927436851c00ebb4b to your computer and use it in GitHub Desktop.
Save Kritner/a4165cbdb686364927436851c00ebb4b to your computer and use it in GitHub Desktop.
List of tuples for factory branching removal
public interface IFoo { }
public class FooOne : IFoo { }
public class FooTwo : IFoo { }
public class FooFourtyTwo : IFoo { }
public class FooFace : IFoo { }
public class FooNull : IFoo { }
public class Whatever
{
public IFoo GetFoo(bool someFlag, string someLabel)
{
if (someFlag)
{
if (someLabel == "Test"
return new FooOne();
if (someLabel == "blah blah")
return new FooTwo();
}
else
{
if (someLabel == "Test"
return new FooFourtyTwo();
if (someLabel == "blah blah")
return new FooFace();
}
return new FooNull();
}
}
public interface IFoo { }
public class FooOne : IFoo { }
public class FooTwo : IFoo { }
public class FooFourtyTwo : IFoo { }
public class FooFace : IFoo { }
public class FooNull : IFoo { }
public class Whatever
{
private readonly List<(bool someFlag, string someLabel, IFoo someType)> myTypes = new List<(bool someFlag, string someLabel, IFoo someType)>()
{
(true, "Test", new FooOne()),
(true, "blah blah", new FooTwo()),
(false, "Test", new FooFourtyTwo()),
(false, "blah blah", new FooFace()),
};
public IFoo GetFoo(bool someFlag, string someLabel)
{
var result = myTypes
.FirstOrDefault(w => w.someFlag == someFlag && w.someLabel == someLabel)
.someType;
if (result == null)
return new FooNull();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment