Advertisement
Caminhoneiro

Adapter

Oct 2nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. //Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
  2.  
  3.  
  4.  
  5.  
  6. //1 Make sure that you have two actors:
  7. // >Useful service objects.
  8.  
  9. // >Application code that has to use service objects.The application should not be able to use the service objects directly, because of incompatible interfaces or data formats.
  10.  
  11. //2 Declare the client interface that the future adapter class will be following.The application will use this interface to communicate with an adapter.
  12.  
  13. //3 Create an adapter class, make it implementing the client interface, but leave all methods empty for now.
  14.  
  15. //4 Add a field into the adapter class that will store a reference to a service object. In most cases, this field gets the value in a constructor. In simpler cases, the adaptee can be passed directly to the adapter methods.
  16.  
  17. //5 One by one, implement all methods of the client interface in adapter class. These methods should pass execution to appropriate methods of the service object, while converting any passed data to a proper format.
  18.  
  19. //6 Once the adapter class is ready, use it in application code via the client interface
  20.  
  21. using System;
  22. namespace Adapter
  23. {
  24.     //1 ...
  25.     //2 Declare the client interface that the future adapter class will be following.The application will use this interface to communicate with an adapter.
  26.     interface ITarget //// >Useful service objects.
  27.     {
  28.         string GetRequest();
  29.     }
  30.     class Adaptee // >Application code that has to use service objects.The application should not be able to use the service objects directly, because of incompatible interfaces or data formats.
  31.     {
  32.         public string GetSpecificRequest()
  33.         {
  34.             return "Specific request.";
  35.         }
  36.     }
  37.  
  38.     //3 Create an adapter class, make it implementing the client interface.
  39.     class Adapter : ITarget
  40.     {
  41.         //4 Add a field into the adapter class that will store a reference to a service object. In most cases, this field gets the value in a constructor.
  42.         //In simpler cases, the adaptee can be passed directly to the adapter methods.
  43.  
  44.         private readonly Adaptee _adaptee;
  45.  
  46.         public Adapter(Adaptee adaptee)
  47.         {
  48.             _adaptee = adaptee;
  49.         }
  50.  
  51.         //5 One by one, implement all methods of the client interface in adapter class.
  52.         //These methods should pass execution to appropriate methods of the service object, while converting any passed data to a proper format.
  53.         public string GetRequest()
  54.         {
  55.             return $"This is '{_adaptee.GetSpecificRequest()}'";
  56.         }
  57.     }
  58.     class Client
  59.     {
  60.         public void Main()
  61.         {
  62.             Adaptee adaptee = new Adaptee();
  63.  
  64.             ITarget target = new Adapter(adaptee);
  65.  
  66.             Console.WriteLine("Adaptee interface is incompatible with the client.");
  67.             Console.WriteLine("But with adapter client can call it's method.");
  68.  
  69.             Console.WriteLine(target.GetRequest());
  70.         }
  71.     }
  72.  
  73.  
  74.     class Program
  75.     {
  76.         static void Main(string[] args)
  77.         {
  78.             Console.WriteLine("Adapter Start!");
  79.  
  80.             new Client().Main();
  81.  
  82.             Console.ReadKey();
  83.            
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement