Created
January 26, 2010 17:57
-
-
Save actaneon/287046 to your computer and use it in GitHub Desktop.
Factory Using Closure to Configure at Startup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using StructureMap; | |
public class Order | |
{ | |
} | |
interface IOrderShipper | |
{ | |
void Ship(Order order); | |
} | |
public class OrderProcessor | |
{ | |
public void Process(Order order) | |
{ | |
IOrderShipper shipper = new OrderShipperFactory().GetDefault(); | |
shipper.Ship(order); | |
} | |
} | |
public class OrderShipperFactory | |
{ | |
public static Func<IOrderShipper> CreationClosure; | |
public IOrderShipper GetDefault() | |
{ | |
return CreationClosure; //executes closure | |
} | |
} | |
public class GenericFactory | |
{ | |
public static Func<object> CreationClosure; | |
public T GetDefault<T>() | |
{ | |
return (T)CreationClosure; //executes closure | |
} | |
} | |
public class Main | |
{ | |
private static void ConfigureFactories() | |
{ | |
OrderShipperFactory.CreationClosure = | |
() => ObjectFactory.GetInstance<IOrderShipper>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment