BorisKotlyar

Untitled

Jun 18th, 2020
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. namespace DefaultNamespace
  2. {
  3.     public abstract class Factory<T>
  4.     {
  5.         public abstract T Create();
  6.     }
  7.    
  8.     public abstract class TransportFabric<T> : Factory<T> where T : ITransport { }
  9.  
  10.     public class TruckFabric : TransportFabric<Truck>
  11.     {
  12.         public override Truck Create()
  13.         {
  14.             return new Truck();
  15.         }
  16.     }
  17.  
  18.     public class CarFabrick : TransportFabric<Car>
  19.     {
  20.         public override Car Create()
  21.         {
  22.             return new Car();
  23.         }
  24.     }
  25.  
  26.     public interface ITransport { }
  27.     public class Truck : ITransport { }
  28.     public class Car : ITransport { }
  29.    
  30.     public class Foo
  31.     {
  32.         private ITransport _truck;
  33.         private ITransport _car;
  34.        
  35.         public Foo(TransportFabric<Truck> truckFactory, TransportFabric<Car> carFactory)
  36.         {
  37.             _truck = truckFactory.Create();
  38.             _car = carFactory.Create();
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment