Seal_of_approval

Builder

Nov 17th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace test
  4. {
  5.     public class Person
  6.     {
  7.         public string Name { get; set;}
  8.         public int Age { get; set;}
  9.  
  10.         public string StreetAddress { get; set;}
  11.         public int PostCode { get; set;}
  12.         public string Country { get; set;}
  13.  
  14.         public string CompanyName { get; set;}
  15.         public string Position { get; set;}
  16.         public int YearsOfWork { get; set;}
  17.  
  18.  
  19.         public static PersonBuilder Create()
  20.         {
  21.             return new PersonBuilder (new Person ());
  22.         }
  23.  
  24.         public void ShowPerson()
  25.         {
  26.             Console.WriteLine ("Name: {0} \nAge: {1}, \nAddress: {2}, {3}, {4} city\nWork/Study at: {5}, as a {6}, {7} years",
  27.                 this.Name, this.Age, this.StreetAddress, this.PostCode, this.Country, this.CompanyName, this.Position, this.YearsOfWork);
  28.         }
  29.     }
  30.  
  31.     public class PersonAddressBuilder
  32.     {
  33.         private Person person;
  34.         public PersonAddressBuilder (Person person)
  35.         {
  36.             this.person = person;
  37.         }
  38.  
  39.         public PersonAddressBuilder At (string streetAddress)
  40.         {
  41.             person.StreetAddress = streetAddress;
  42.             return this;
  43.         }
  44.  
  45.         public PersonAddressBuilder WithPostCode (int postCode)
  46.         {
  47.             person.PostCode = postCode;
  48.             return this;
  49.         }
  50.  
  51.         public PersonAddressBuilder In (string country)
  52.         {
  53.             person.Country = country;
  54.             return this;
  55.         }
  56.  
  57.         public PersonAddressBuilder Lives
  58.         {
  59.             get { return new PersonAddressBuilder(person); }
  60.         }
  61.  
  62.         public static implicit operator Person (PersonAddressBuilder builder)
  63.         {
  64.             return builder.person;
  65.         }
  66.  
  67.         public PersonJobBuilder Works
  68.         {
  69.             get { return new PersonJobBuilder (person);}
  70.         }
  71.     }
  72.  
  73.     public class PersonJobBuilder
  74.     {
  75.         private Person person;
  76.         public PersonJobBuilder(Person person)
  77.         {
  78.             this.person = person;
  79.         }
  80.  
  81.         public PersonJobBuilder At (string companyName)
  82.         {
  83.             person.CompanyName = companyName;
  84.             return this;
  85.         }
  86.  
  87.         public PersonJobBuilder AsA (string position)
  88.         {
  89.             person.Position = position;
  90.             return this;
  91.         }
  92.  
  93.         public PersonJobBuilder Years(int yearsOfWork)
  94.         {
  95.             person.YearsOfWork = yearsOfWork;
  96.             return this;
  97.         }
  98.            
  99.         public static implicit operator Person (PersonJobBuilder builder)
  100.         {
  101.             return builder.person;
  102.         }
  103.            
  104.     }
  105.  
  106.     public class PersonBuilder
  107.     {
  108.         private Person person;
  109.  
  110.         public PersonBuilder(Person person)
  111.         {
  112.             this.person = person;
  113.         }
  114.  
  115.         public PersonBuilder Called (string name)
  116.         {
  117.             person.Name = name;
  118.             return this;
  119.         }
  120.  
  121.         public PersonBuilder Age (int age)
  122.         {
  123.             person.Age = age;
  124.             return this;
  125.         }
  126.  
  127.         public static implicit operator Person (PersonBuilder pb)
  128.         {
  129.             return pb.person;
  130.         }
  131.  
  132.         public PersonAddressBuilder Lives
  133.         {
  134.             get { return new PersonAddressBuilder(person); }
  135.         }
  136.            
  137.         public PersonJobBuilder Works
  138.         {
  139.             get { return new PersonJobBuilder (person);}
  140.         }
  141.            
  142.     }
  143.  
  144.     public class MainClass
  145.     {
  146.         public static void Main (string[] args)
  147.         {
  148.             Person p_1 = Person.Create ().Called ("Ma").Age (20)
  149.                 .Lives.At ("V, flat 2").WithPostCode (8).In ("S")
  150.                 .Works.At("S").AsA("student").Years(3);
  151.  
  152.             Person p_2 = Person.Create ().Called ("Ir").Age (41)
  153.                 .Lives.At ("Vo, flat 2").WithPostCode (2).In ("S")
  154.                 .Works.At("T").AsA("Manager").Years(20);
  155.  
  156.             p_1.ShowPerson ();
  157.             Console.WriteLine ("_____________________");
  158.             p_2.ShowPerson ();
  159.  
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment