我想知道我是否正確理解這種模式。假設我們有接口不兼容。 函數來自第三方dll的Printer()需要IHPPrinter,但我們的對象沒有實現這種干涉...所以最後我們必須實現這個接口並返回我們的方法的實現,就像在下面的代碼中一樣? :) 第二個問題是,如果我們不爲方法DocumentsInQueue提供實現,將會拋出異常。是否有可能阻止調用這種方法?C中的適配器模式#
class Program
{
static void Main(string[] args)
{
EpsonPrinter _epsonPrinter = new EpsonPrinter();
Printer(_epsonPrinter);
Console.ReadKey();
}
public static void Printer(IHPPrinter hpPrinter)
{
hpPrinter.PrintDocument();
}
public interface IHPPrinter
{
void PrintDocument();
int DocumentsInQueue();
}
public interface IEpsonPrinter
{
void Print();
}
public class EpsonPrinter : IEpsonPrinter, IHPPrinter
{
public int DocumentsInQueue()
{
throw new NotImplementedException();
}
public void Print()
{
this.PrintDocument();
}
public void PrintDocument()
{
Console.WriteLine("Printing from Epson printer...");
}
}
}
您還沒有實現適配器模式,基本上是要實現在同一類兩個接口。 –