Advertisement
Caminhoneiro

Factory Method

Oct 2nd, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1.  
  2. //FACTORY METHOD
  3. //Ref link https://refactoring.guru/design-patterns/factory-method
  4.  
  5. //1 Extract the common interface for all products.This interface should declare methods that make sense for every product.
  6.  
  7. //2  Add an empty factory method inside the creator class. Its signature should return the product interface type.
  8.  
  9. //3  Go over the creator's code and find all references to product constructors. One by one, replace them with calls to the factory method but extract product creation code to the factory method.
  10. //   You might need to add a temporary parameter to the factory method that will be used to control which product will be created.
  11. //   At this point, the factory method's code may look pretty ugly. It may have a large switch operator that picks which product class will be instantiated.
  12. //   But do not worry, we will fix it right away.
  13.  
  14. //4  Now, override the factory method in subclasses and move there the appropriate case from the switch operator in the base method.
  15.  
  16. //5 The control parameter used in the base creator's class can also be used in subclasses.
  17. //For instance, you may have a creator's hierarchy with a base class Mail and classes Air and Ground, plus the product classes: Plane, Truck and Train.
  18. //Air matches Plane just fine, but Ground matches both Truck and Train at the same time. You can create a new subclass to handle both cases, but there is another option.
  19. //The client code can pass an argument to the factory method of the Ground class to control which product it receives.
  20.  
  21. //6 If the base factory method has become empty after all moves, you can make it abstract.
  22.  
  23.    
  24. using System;
  25. namespace Factory
  26. {
  27.  
  28.     //1 Extract the common interface for all products.
  29.     interface IProduct
  30.     {
  31.         //1.1 This interface should declare methods that make sense for every product.
  32.         string Operation();
  33.     }
  34.  
  35.     //2  Add an empty factory method inside the creator class.
  36.     abstract class ProductCreator
  37.     {
  38.         //2.1 Its signature should return the product interface type.
  39.         public abstract IProduct FactoryMethod();
  40.        
  41.         //3
  42.         public string SomeOperation()
  43.         {
  44.             var product = FactoryMethod();
  45.  
  46.             var result = "Creator: The same creator's code has just worked with " + product.Operation();
  47.  
  48.             return result;
  49.         }
  50.  
  51.     }
  52.  
  53.     //4  Now, override the factory method in subclasses and move there the appropriate case from the switch operator in the base method.
  54.     class TruckCreator : ProductCreator
  55.     {
  56.         public override IProduct FactoryMethod()
  57.         {
  58.             return new ConcreteTruck();
  59.         }
  60.     }
  61.  
  62.     class ShipCreator: ProductCreator
  63.     {
  64.         public override IProduct FactoryMethod()
  65.         {
  66.             return new ConcreteShip();
  67.         }
  68.     }
  69.  
  70.     class AirPlaneCreator : ProductCreator
  71.     {
  72.         public override IProduct FactoryMethod()
  73.         {
  74.             return new ConcreteAirPlane();
  75.         }
  76.     }
  77.  
  78.  
  79.     //5
  80.     class ConcreteTruck : IProduct
  81.     {
  82.         public string Operation()
  83.         {
  84.             return "{Truck created!}";
  85.         }
  86.     }
  87.  
  88.     class ConcreteShip : IProduct
  89.     {
  90.         public string Operation()
  91.         {
  92.             return "{Ship created!}";
  93.         }
  94.     }
  95.  
  96.     class ConcreteAirPlane : IProduct
  97.     {
  98.         public string Operation()
  99.         {
  100.             return "{AirPlane created!}";
  101.         }
  102.     }
  103.  
  104.     //6 CLIENT
  105.     public class Client
  106.     {
  107.         void ClientMethod(ProductCreator creator)
  108.         {
  109.             // ...
  110.             Console.WriteLine("Client: I'm not aware of the creator's class, but it still works.\n"
  111.                               + creator.SomeOperation());
  112.             // ...
  113.         }
  114.  
  115.         public void Main()
  116.         {
  117.             Console.WriteLine("App: Launched with the TruckCreator.");
  118.             ClientMethod(new TruckCreator());
  119.  
  120.             Console.WriteLine("");
  121.  
  122.             Console.WriteLine("App: Launched with the ShipCreator.");
  123.             ClientMethod(new ShipCreator());
  124.         }
  125.  
  126.     }
  127.  
  128.     class Program
  129.     {
  130.         static void Main(string[] args)
  131.         {
  132.             new Client().Main();
  133.  
  134.             Console.WriteLine("Start");
  135.  
  136.             Console.ReadKey();
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement