我正在学习适配器模式,并使用以下链接查看代码。我的代码和示例代码的区别在于,我删除了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。很好的解释。谢谢。 –