호환되지 않는 객체들이 협업할 수 있도록 해주는 구조 디자인 패턴이다. 어댑터는 두 객체 사이의 래퍼 역할을 한다.
public interface ITarget
{
string GetRequest();
}
class Adaptee
{
public string GetSpecificRequest()
{
return "Specific request.";
}
}
class Adapter : ITarget
{
private readonly Adaptee _adaptee;
public Adapter(Adaptee adaptee)
{
this._adaptee = adaptee;
}
// 래핑 (rapping)
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());
}
}
출력
Adaptee interface is incompatible with the client.
But with adapter client can call it's method.
This is 'Specific request.'