Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             // путешественник
  6.             Driver driver = new Driver();
  7.             // машина
  8.             Auto auto = new Auto();
  9.             // отправляемся в путешествие
  10.             //driver.Travel(auto);
  11.             // встретились пески, надо использовать верблюда
  12.             Camel camel = new Camel();
  13.             driver.Travel(camel);
  14.             // используем адаптер
  15.             //ITransport camelTransport = new CamelToTransportAdapter(camel);
  16.             //// продолжаем путь по пескам пустыни
  17.             //driver.Travel(camelTransport);
  18.             IAnimal autoCamel = new TransportAdapterToCamel(auto);
  19.             driver.Travel(autoCamel);
  20.  
  21.             Console.Read();
  22.         }
  23.     }
  24.     interface ITransport
  25.     {
  26.         void Drive();
  27.     }
  28.     // класс машины
  29.     class Auto : ITransport
  30.     {
  31.         public void Drive()
  32.         {
  33.             Console.WriteLine("Машина едет по дороге");
  34.         }
  35.     }
  36.     class Driver
  37.     {
  38.         public void Travel(IAnimal transport)
  39.         {
  40.             transport.Move();
  41.         }
  42.     }
  43.     // интерфейс животного
  44.     interface IAnimal
  45.     {
  46.         void Move();
  47.     }
  48.     // класс верблюда
  49.     class Camel : IAnimal
  50.     {
  51.         public void Move()
  52.         {
  53.             Console.WriteLine("Верблюд идет по пескам пустыни");
  54.         }
  55.     }
  56.     // Адаптер от Camel к ITransport
  57.     class CamelToTransportAdapter : ITransport
  58.     {
  59.         Camel camel;
  60.         public CamelToTransportAdapter(Camel c)
  61.         {
  62.             camel = c;
  63.         }
  64.  
  65.         public void Drive()
  66.         {
  67.             camel.Move();
  68.         }
  69.     }
  70.     class TransportAdapterToCamel : IAnimal
  71.     {
  72.         Auto auto;
  73.         public TransportAdapterToCamel(Auto c)
  74.         {
  75.             auto = c;
  76.         }
  77.  
  78.         public void Move()
  79.         {
  80.             auto.Drive();
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement