Advertisement
MaksNew

Untitled

Mar 1st, 2022
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 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.         // используем адаптер
  14.         ITransport camelTransport = new CamelToTransportAdapter(camel);
  15.         // продолжаем путь по пескам пустыни
  16.         driver.Travel(camelTransport);
  17.  
  18.         Console.Read();
  19.     }
  20. }
  21. interface ITransport
  22. {
  23.     void Drive();
  24. }
  25. // класс машины
  26. class Auto : ITransport
  27. {
  28.     public void Drive()
  29.     {
  30.         Console.WriteLine("Машина едет по дороге");
  31.     }
  32. }
  33. class Driver
  34. {
  35.     public void Travel(ITransport transport)
  36.     {
  37.         transport.Drive();
  38.     }
  39. }
  40. // интерфейс животного
  41. interface IAnimal
  42. {
  43.     void Move();
  44. }
  45. // класс верблюда
  46. class Camel : IAnimal
  47. {
  48.     public void Move()
  49.     {
  50.         Console.WriteLine("Верблюд идет по пескам пустыни");
  51.     }
  52. }
  53. // Адаптер от Camel к ITransport
  54. class CamelToTransportAdapter : ITransport
  55. {
  56.     Camel camel;
  57.     public CamelToTransportAdapter(Camel c)
  58.     {
  59.         camel = c;
  60.     }
  61.  
  62.     public void Drive()
  63.     {
  64.         camel.Move();
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement