Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. internal delegate T Instantiate<out T>(/*parameters*/) where T : Vehicle;
  2.  
  3. internal interface IVehicle{/*...*/}
  4. public class Vehicle
  5. {
  6. //constructor is not protected. so this cant be intertied from outside assembly.
  7. internal Vehicle(/*parameters*/){ }
  8. }
  9. public class Car : Vehicle
  10. {
  11. internal Car(/*parameters*/) : base(/*parameters*/) { }
  12. }
  13.  
  14. /// <summary>
  15. /// Choose from one of predefined containers to create your vehicle from factoy.
  16. /// </summary>
  17. /// <typeparam name="T">type of vehicle</typeparam>
  18. public class InstanceContainer<T> where T : Vehicle
  19. {
  20. internal InstanceContainer(Instantiate<T> instance) // internal
  21. {
  22. GetInstance = instance;
  23. }
  24.  
  25. // delegate which creates new instance of T
  26. internal Instantiate<T> GetInstance { get; }
  27. }
  28.  
  29. /// <summary>
  30. /// Choose this to create car from factory.
  31. /// </summary>
  32. public static InstanceContainer<Car> CarInstance { get; } = new InstanceContainer<Car>(CreateCar);
  33.  
  34. private static readonly Instantiate<Car> CreateCar = (/*parameters*/) => new Car(/*parameters*/);
  35.  
  36. // ... others options like Plane, Ship etc
  37.  
  38. public static T CreateVehicle<T>(/*parameters,*/ InstanceContainer<T> instance) where T : Vehicle
  39. {
  40. T obj;
  41. //...
  42. // At some point:
  43. obj = instance.GetInstance(/*parameters*/);
  44. //...
  45. return obj;
  46. }
  47.  
  48. VehicleFactory.CreateVehicle(/*parameters,*/ VehicleFactory.CarInstance);
  49.  
  50. // turns long name into short one!
  51. using VehicleInstance = Factories.VehicleFactory.InstanceContainer<Factories.Goods.Vehicle>
  52.  
  53. //...
  54. //...
  55.  
  56. VehicleInstance instance;
  57. // instance will be choosen based on some conditions. like this:
  58. if(true)
  59. instance = VehicleFactory.CarInstance; // we got magic co-variant here!
  60. else
  61. instance = VehicleFactory.PlaneInstance;
  62.  
  63. VehicleFactory.CreateVehicle(/*parameters,*/ instance);
  64.  
  65. public interface IContainer<out T>
  66. {
  67. // empty!
  68. }
  69.  
  70. internal class InstanceContainer<T> : IContainer<T> where T : Vehicle
  71. {
  72. internal InstanceContainer(Instance<T> instance)
  73. {
  74. GetInstance = instance;
  75. }
  76.  
  77. internal Instance<T> GetInstance { get; }
  78. }
  79.  
  80. // works fine!
  81. public static IContainer<Car> CarInstance { get; } = new InstanceContainer<Car>(CreateCar);
  82.  
  83. public static T CreateVehicle<T>(/*parameters,*/ IContainer<T> instance)
  84. {
  85. T obj;
  86. //...
  87. // At some point:
  88. obj = ((InstanceContainer<T>)instance).GetInstance(/*parameters*/); // runtime error.
  89. //...
  90. return obj;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement