我正在學習適配器模式,並使用以下鏈接查看代碼。我的代碼和示例代碼的區別在於,我刪除了ITarget接口,並直接在Client中創建對象。實現適配器模式的困惑
我知道使用接口的重要性,但是否真的有必要使用的界面,更具體地說,是沒有創建界面,我是違反適配器模式規則?
我的代碼(不包括接口)
class Program
{
static void Main(string[] args)
{
Adapter obj = new Adapter();
Client client = new Client(obj);
client.MakeRequest();
}
}
public class Client
{
private Adapter _target;
public Client(Adapter target)
{
_target = target;
}
public void MakeRequest()
{
_target.MethodA();
}
}
public class Adaptee
{
public void MethodB()
{
Console.WriteLine("MethodB called");
}
}
public class Adapter
{
Adaptee _adaptee = new Adaptee();
public void MethodA()
{
_adaptee.MethodB();
}
}
感謝。
+1。很好的解釋。謝謝。 –