code_junkie

DRYing up object instantiation in C#

Nov 11th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. public class WidgetPlatform
  2. {
  3. public Widget LeftmostWidget { get; set; }
  4. public Widget RightmostWidget { get; set; }
  5.  
  6. public String GetWidgetNames()
  7. {
  8. return LeftmostWidget.Name + " " + RightmostWidget.Name;
  9. }
  10. }
  11.  
  12. public class WidgetPlatform
  13. {
  14. [Required]
  15. public Widget LeftmostWidget { get; set; }
  16.  
  17. [Required]
  18. public Widget RightmostWidget { get; set; }
  19.  
  20. public String GetWidgetNames()
  21. {
  22. return LeftmostWidget.Name + " " + RightmostWidget.Name;
  23. }
  24. }
  25.  
  26. public class WidgetPlatform
  27. {
  28. public Widget LeftmostWidget { get; set; }
  29. public Widget RightmostWidget { get; set; }
  30.  
  31. public WidgetPlatform()
  32. {
  33. this.LeftMostWidget = new Widget();
  34. this.RightMostWidget = new Widget();
  35. }
  36.  
  37. public WidgetPlatform(Widget left, Widget right)
  38. {
  39. if(left == null || right == null)
  40. throw new ArgumentNullException("Eeep!");
  41.  
  42. this.LeftMostWidget = left;
  43. this.RightMostWidget = right;
  44. }
  45.  
  46.  
  47. public String GetWidgetNames()
  48. {
  49. return LeftmostWidget.Name + " " + RightmostWidget.Name;
  50. }
  51. }
  52.  
  53. public class WidgetPlatform
  54. {
  55. public Widget LeftWidget
  56. {
  57. get;
  58. private set;
  59. }
  60.  
  61. public Widget RightWidget
  62. {
  63. get;
  64. private set;
  65. }
  66.  
  67. WidgetPlatForm(Widget w1, Widget w2)
  68. {
  69. if (w1 == null || w2 == null)
  70. throw new ArgumentException();
  71.  
  72. this.LeftWidget = w1;
  73. this.RightWidget = w2;
  74. }
  75.  
  76. // Etc
  77. }
  78.  
  79. public class WidgetPlatform
  80. {
  81. /// <summary>
  82. /// Hide the constructor.
  83. /// </summary>
  84. private WidgetPlatform(Widget left, Widget right)
  85. {
  86. this.LeftmostWidget = left;
  87. this.RightmostWidget = right;
  88. }
  89.  
  90. public Widget LeftmostWidget
  91. {
  92. get;
  93. private set;
  94. }
  95.  
  96. public Widget RightmostWidget
  97. {
  98. get;
  99. private set;
  100. }
  101.  
  102. public static WidgetPlatform GetInstance(Widget left, Widget right)
  103. {
  104. return new WidgetPlatform(left, right);
  105. }
  106. }
Add Comment
Please, Sign In to add comment