code_junkie

Is it a good idea to have a factory class using generics to instantiate objects?

Nov 12th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. abstract class Animal
  2. {
  3. public abstract void MakeSound();
  4. }
  5.  
  6. class Cat : Animal
  7. {
  8. public override void MakeSound()
  9. {
  10. Console.Write("Mew mew");
  11. }
  12. }
  13.  
  14. class Dog : Animal
  15. {
  16. public override void MakeSound()
  17. {
  18. Console.Write("Woof woof");
  19. }
  20. }
  21.  
  22. static class AnimalFactory
  23. {
  24. public static T Create<T>() where T : Animal, new()
  25. {
  26. return new T();
  27. }
  28. }
  29.  
  30. class Program
  31. {
  32. static void Main(string[] args)
  33. {
  34. Dog d = AnimalFactory.Create<Dog>();
  35. d.MakeSound();
  36. }
  37. }
  38.  
  39. class Foo
  40. {
  41. static Foo<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
  42. {...}
  43. }
  44.  
  45. class Foo<TKey>
  46. {
  47. static Foo<TKey, TValue> Create<TValue>(TValue value)
  48. {...}
  49. }
  50.  
  51. class Foo<TKey, TValue>
  52. {
  53. }
  54.  
  55. Foo.Create(x, y);
  56. Foo<string>.Create(y);
  57. new Foo<string, int>(x, y);
  58.  
  59. public class Factory
  60. {
  61. public static T Create<T>() where T : ParentClass, new(String)
  62. {
  63.  
  64. }
  65. }
  66.  
  67. public class Factory
  68. {
  69. public static T Create<T>() where T : ParentClass
  70. {
  71. T child = new T("hi")
  72. }
  73. }
  74.  
  75. public class Factory
  76. {
  77. public static T Create<T>() where T : ParentClass, new()
  78. {
  79. T child;
  80.  
  81. child = new T();
  82.  
  83. if (child is ChildClass)
  84. {
  85. child = new ChildClass("hi");
  86. }
  87.  
  88. return child;
  89. }
  90. }
  91.  
  92. static class AnimalFactory
  93. {
  94. public static Animal Create<T>() where T : Animal
  95. {
  96. return Create<T>("blue");
  97. }
  98.  
  99. public static Animal Create<T>(string colour) where T : Animal, new()
  100. {
  101. return new T() {Colour = colour};
  102. }
  103. }
  104.  
  105. Factory<IAwesome> awesomeFactory = new Factory<IAwesome>();
  106. IAwesome awesomeObject = awesomeFactory.Load(configValue);
  107.  
  108. public interface IFactory<T>
  109. {
  110. T Create();
  111. }
  112.  
  113. public class DefaultConstructorFactory<T> : IFactory<T> where T : new()
  114. {
  115. public T Create()
  116. {
  117. return new T();
  118. }
  119. }
  120.  
  121. public class ActivatorFactory<T> : IFactory<T>
  122. {
  123. public T Create()
  124. {
  125. return Activator.CreateInstance<T>();
  126. }
  127. }
  128.  
  129. public class AnimalTamer<TAnimal> where TAnimal : Animal
  130. {
  131. IFactory<TAnimal> _animalFactory;
  132.  
  133. public AnimalTamer(IFactory<TAnimal> animalFactory)
  134. {
  135. if(animalFactory == null)
  136. {
  137. throw new ArgumentNullException("animalFactory");
  138. }
  139.  
  140. _animalFactory= animalFactory;
  141. }
  142.  
  143. public void PutOnShow()
  144. {
  145. var animal = _animalFactory.Create();
  146.  
  147. animal.MakeSound();
  148. }
  149. }
  150.  
  151. class Program
  152. {
  153. static void Main(string[] args)
  154. {
  155. var tamer = new AnimalTamer<Tiger>(new DefaultConstructorFactory<Tiger>());
  156.  
  157. tamer.PutOnShow();
  158. }
  159. }
Add Comment
Please, Sign In to add comment