Caminhoneiro

Builder

Oct 2nd, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //1 Make sure that you have the common steps of building the product, as well as variations of the steps that lead to the creation of various representations of products.
  8.  
  9. //2 Create the Builder interface class and declare production steps in it.
  10.  
  11. //3 Create a Concrete Builder class for each of the product representations.Implement their construction steps.
  12.  
  13. //4 Think about creating a Director class. Its methods should create different product configurations, using different steps of the same builder instance.
  14.  
  15. //5 The client code creates both Builder and Director objects. It creates a builder instance first and then passes it either to the director's constructor or its production methods.
  16.  
  17. //6 The client should call a production method of a Director object to begin the construction process.
  18.  
  19. //7 The result can be obtained from the Director object only if all products have a common interface. In the opposite case, each Builder must have its own method of retrieving the result.
  20.  
  21.  
  22. namespace Builder
  23. {
  24.     //1 Make sure that you have the common steps of building the product, as well as variations of the steps that lead to the creation of various representations of products.
  25.     public class Product
  26.     {
  27.         List<object> parts = new List<object>();
  28.  
  29.         public void Add(string part)
  30.         {
  31.             parts.Add(part);
  32.         }
  33.  
  34.         public string ListParts()
  35.         {
  36.             string str = string.Empty;
  37.  
  38.             for (int i = 0; i < parts.Count; i++)
  39.             {
  40.                 str += parts[i] + ", ";
  41.             }
  42.  
  43.             str = str.Remove(str.Length - 2); // removing last ",c"
  44.  
  45.             return "Product parts: " + str + "\n";
  46.         }
  47.     }
  48.  
  49.  
  50.     //2 Create the Builder interface class and declare production steps in it.
  51.     public abstract class Builder
  52.     {
  53.         public abstract void BuildPartA();
  54.  
  55.         public abstract void BuildPartB();
  56.  
  57.         public abstract void BuildPartC();
  58.  
  59.         public abstract Product GetProduct();
  60.     }
  61.  
  62.  
  63.     //3 Create a Concrete Builder class for each of the product representations.Implement their construction steps.
  64.  
  65.     public class ConcreteBuilder : Builder
  66.     {
  67.         Product product = new Product();
  68.  
  69.         public override void BuildPartA()
  70.         {
  71.             product.Add("PartA1");
  72.         }
  73.  
  74.         public override void BuildPartB()
  75.         {
  76.             product.Add("PartB1");
  77.         }
  78.  
  79.         public override void BuildPartC()
  80.         {
  81.             product.Add("PartC1");
  82.         }
  83.  
  84.         public override Product GetProduct()
  85.         {
  86.             Product result = product;
  87.  
  88.             this.Reset();
  89.  
  90.             return result;
  91.         }
  92.  
  93.         public void Reset()
  94.         {
  95.             product = new Product();
  96.         }
  97.     }
  98.  
  99.     //4 Think about creating a Director class. Its methods should create different product configurations, using different steps of the same builder instance.
  100.     public class Director
  101.     {
  102.         Builder builder;
  103.  
  104.         public Director(Builder builder)
  105.         {
  106.             this.builder = builder;
  107.         }
  108.  
  109.         public void buildMinimalViableProduct()
  110.         {
  111.             builder.BuildPartA();
  112.         }
  113.  
  114.         public void buildFullFeaturedProduct()
  115.         {
  116.             builder.BuildPartA();
  117.             builder.BuildPartB();
  118.             builder.BuildPartC();
  119.         }
  120.     }
  121.  
  122.     //5 The client code creates both Builder and Director objects. It creates a builder instance first and then passes it either to the director's constructor or its production methods.
  123.     public class Client
  124.     {
  125.         //6 The client should call a production method of a Director object to begin the construction process.
  126.         public void ClientCode(Director director, Builder builder)
  127.         {
  128.             Console.WriteLine("Standart basic product:");
  129.             director.buildMinimalViableProduct();
  130.             Console.WriteLine(builder.GetProduct().ListParts());
  131.  
  132.             Console.WriteLine("Standart full featured product:");
  133.             director.buildFullFeaturedProduct();
  134.             Console.WriteLine(builder.GetProduct().ListParts());
  135.  
  136.             Console.WriteLine("Custom product:");
  137.             builder.BuildPartA();
  138.             builder.BuildPartC();
  139.             Console.WriteLine(builder.GetProduct().ListParts());
  140.         }
  141.     }
  142.    
  143.  
  144.     //7 The result can be obtained from the Director object only if all products have a common interface. In the opposite case, each Builder must have its own method of retrieving the result.
  145.     class Program
  146.     {
  147.         static void Main(string[] args)
  148.         {
  149.             Console.WriteLine("Builder start!");
  150.            
  151.             Builder builder = new ConcreteBuilder();
  152.             Director director = new Director(builder);
  153.  
  154.             Client client = new Client();
  155.             client.ClientCode(director, builder);
  156.  
  157.             Console.ReadKey();
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment