code_junkie

How do you force constructor signatures and static methods?

Nov 11th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. interface Fruit{}
  2.  
  3. interface FruitFactory<F extends Fruit>{
  4. F newFruit(String color,double weight);
  5.  
  6. Cocktail mixFruits(F f1,F f2);
  7. }
  8.  
  9. class Apple implements Fruit{}
  10. class AppleFactory implements FruitFactory<Apple>{
  11. public Apple newFruit(String color, double weight){
  12. // create an instance
  13. }
  14. public Cocktail mixFruits(Apple f1,Apple f2){
  15. // implementation
  16. }
  17. }
  18.  
  19. class Base
  20. {
  21. private Base() { }
  22. public Base(int x) {}
  23. }
  24.  
  25. class Derived : Base
  26. {
  27. //public Derived() { } won't compile because Base() is private
  28. public Derived(int x) :base(x) {}
  29. public Derived() : base (0) {} // still works because you are giving a value to base
  30. }
  31.  
  32. interface IFoo
  33. {
  34. IFoo(int gottaHaveThis);
  35. static Bar();
  36. }
  37.  
  38. interface ISummable
  39. {
  40. operator+(ISummable a, ISummable b);
  41. }
  42.  
  43. class Foo: IFoo
  44. {
  45. Foo(int gottaHaveThis) {};
  46. static Bar() {};
  47. }
  48.  
  49. class SonOfFoo: Foo
  50. {
  51. // SonOfFoo(int gottaHaveThis): base(gottaHaveThis); is implicitly defined
  52. }
  53.  
  54. class DaughterOfFoo: Foo
  55. {
  56. DaughhterOfFoo (int gottaHaveThis) {};
  57. }
  58.  
  59. ISummable PassedFirstGrade = (ISummable) 10;
  60.  
  61. class Program
  62. {
  63. static void Main(string[] args)
  64. {
  65. Console.WriteLine(Foo.Instance.GetHelloWorld());
  66. Console.ReadLine();
  67. }
  68. }
  69.  
  70. public class Foo : FooStaticContract<FooFactory>
  71. {
  72. public Foo() // Non-static ctor.
  73. {
  74. }
  75.  
  76. internal Foo(bool st) // Overloaded, parameter not used.
  77. {
  78. }
  79.  
  80. public override string GetHelloWorld()
  81. {
  82. return "Hello World";
  83. }
  84. }
  85.  
  86. public class FooFactory : IStaticContractFactory<Foo>
  87. {
  88. #region StaticContractFactory<Foo> Members
  89.  
  90. public Foo CreateInstance()
  91. {
  92. return new Foo(true); // Call static ctor.
  93. }
  94.  
  95. #endregion
  96. }
  97.  
  98. public interface IStaticContractFactory<T>
  99. {
  100. T CreateInstance();
  101. }
  102.  
  103. public abstract class StaticContract<T, Factory>
  104. where Factory : IStaticContractFactory<T>, new()
  105. where T : class
  106. {
  107. private static Factory _factory = new Factory();
  108.  
  109. private static T _instance;
  110. /// <summary>
  111. /// Gets an instance of this class.
  112. /// </summary>
  113. public static T Instance
  114. {
  115. get
  116. {
  117. // Scary.
  118. if (Interlocked.CompareExchange(ref _instance, null, null) == null)
  119. {
  120. T instance = _factory.CreateInstance();
  121. Interlocked.CompareExchange(ref _instance, instance, null);
  122. }
  123. return _instance;
  124. }
  125. }
  126. }
  127.  
  128. public abstract class FooStaticContract<Factory>
  129. : StaticContract<Foo, Factory>
  130. where Factory : IStaticContractFactory<Foo>, new()
  131. {
  132. public abstract string GetHelloWorld();
  133. }
Add Comment
Please, Sign In to add comment