axeefectushka

Untitled

Feb 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2.  
  3. public sealed class Adam : Male
  4. {
  5.     private static Adam instance;
  6.  
  7.     private Adam() : base()
  8.     {
  9.         Name = "Adam";
  10.     }
  11.     public static Adam GetInstance()
  12.     {
  13.         if (instance == null)
  14.         {
  15.             instance = new Adam();
  16.         }
  17.         return instance;
  18.     }
  19. }
  20. public sealed class Eve : Female
  21. {
  22.     private static Eve instance;
  23.    
  24.     private Eve() : base()
  25.     {
  26.         Name = "Eve";
  27.     }
  28.     public static Eve GetInstance(Adam adamInstance)
  29.     {
  30.         if (adamInstance == null) throw new ArgumentNullException();
  31.  
  32.         if (instance == null)
  33.         {
  34.             instance = new Eve();
  35.         }
  36.         return instance;
  37.     }
  38. }
  39. public class Male : Human
  40. {
  41.     public Male() {}
  42.     public Male(string name, Human mother, Human father)
  43.     {
  44.         if (mother is null) throw new ArgumentNullException();
  45.         if (father is null) throw new ArgumentNullException();
  46.         else
  47.         {
  48.             Name = name;
  49.             Mother = mother;
  50.             Father = father;
  51.         }
  52.     }
  53.  
  54. }
  55. public class Female : Human
  56. {
  57.     public Female() {}
  58.     public Female(string name, Female mother, Male father)
  59.     {
  60.         if(name=="Eve") throw new ArgumentNullException();
  61.         if (mother is null) throw new ArgumentNullException();
  62.         if (father is null) throw new ArgumentNullException();
  63.         else
  64.         {
  65.             Name = name;
  66.             Mother = mother;
  67.             Father = father;
  68.         }
  69.     }
  70. }
  71. public abstract class Human
  72. {
  73.     public string Name;
  74.     public Human Mother { get; set; }
  75.     public Human Father { get; set; }
  76. }
  77. /*
  78.     public void Father_and_mother_are_essential_for_reproduction()
  79.     {
  80.         // There is just 1 way to reproduce
  81.         Assert.AreEqual(1, typeof(Male).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  82.           .Where(x => x.IsPublic || x.IsAssembly).Count());
  83.  
  84.         Assert.AreEqual(1, typeof(Female).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).
  85.           Where(x => x.IsPublic || x.IsAssembly).Count());
  86.        
  87.         var adam = Adam.GetInstance();
  88.         var eve = Eve.GetInstance(adam);
  89.         Assert.Throws<ArgumentNullException>(() => new Male("Seth", null, null));
  90.         Assert.Throws<ArgumentNullException>(()=> new Male("Abel", eve, null));
  91.         Assert.Throws<ArgumentNullException>(() => new Male("Seth", null, adam));
  92.         Assert.Throws<ArgumentNullException>(() => new Female("Azura", null, null));
  93.         Assert.Throws<ArgumentNullException>(() => new Female("Awan", eve, null));
  94.         Assert.Throws<ArgumentNullException>(() => new Female("Dina", null, adam));
  95.         Assert.Throws<ArgumentNullException>(() => new Female("Eve", null, null));
  96.         Assert.Throws<ArgumentNullException>(() => new Male("Adam", null, null));
  97.     }
  98.     */
Add Comment
Please, Sign In to add comment