Created
June 19, 2019 01:36
-
-
Save gmarcial/b0020c5e787058ba579e819845be03d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace RefactoringGuru.DesignPatterns.Adapter.Conceptual | |
{ | |
// The Target defines the domain-specific interface used by the client code. | |
public interface ITarget | |
{ | |
string GetRequest(); | |
} | |
// The Adaptee contains some useful behavior, but its interface is | |
// incompatible with the existing client code. The Adaptee needs some | |
// adaptation before the client code can use it. | |
class Adaptee | |
{ | |
public string GetSpecificRequest() | |
{ | |
return "Specific request."; | |
} | |
} | |
// The Adapter makes the Adaptee's interface compatible with the Target's | |
// interface. | |
class Adapter : ITarget | |
{ | |
private readonly Adaptee _adaptee; | |
public Adapter(Adaptee adaptee) | |
{ | |
this._adaptee = adaptee; | |
} | |
public string GetRequest() | |
{ | |
return $"This is '{this._adaptee.GetSpecificRequest()}'"; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Adaptee adaptee = new Adaptee(); | |
ITarget target = new Adapter(adaptee); | |
Console.WriteLine("Adaptee interface is incompatible with the client."); | |
Console.WriteLine("But with adapter client can call it's method."); | |
Console.WriteLine(target.GetRequest()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment