我想知道我是否正确理解这种模式。假设我们有接口不兼容。 函数来自第三方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...");
}
}
}
您还没有实现适配器模式,基本上是要实现在同一类两个接口。 –