做网站用什么源码,网站三网合一案例,大搜推广,ajax做购物网站适配器模式#xff08;Adapter Pattern#xff09;
适配器模式#xff08;Adapter Pattern#xff09;也称包装样式或者包装(wrapper)。将一个类的接口转接成用户所期待的。适配器模式是一种结构型模式#xff0c;一个适配使得因接口不兼容而不能在一起工作的类工作在一起…适配器模式Adapter Pattern
适配器模式Adapter Pattern也称包装样式或者包装(wrapper)。将一个类的接口转接成用户所期待的。适配器模式是一种结构型模式一个适配使得因接口不兼容而不能在一起工作的类工作在一起做法是将类自己的接口包裹在一个已存在的类中。将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
(1类适配器模式
using System;
namespace ConsoleApplication
{//目标接口//现有的interface ITarget{void request();}//用户所期待的class Adaptee{public void specificRequest(){Console.WriteLine(适配者中的业务代码被调用);}}//类适配器类class ClassAdapter : Adaptee, ITarget{public void request(){specificRequest();}}//客户端代码class Program{public static void Main(String[] args){Console.WriteLine(类适配器模式测试);ITarget target new ClassAdapter();target.request();}}
} (2对象适配器模式
using System;
namespace ConsoleApplication
{//目标接口//现有的interface ITarget{void request();}//用户所期待的class Adaptee{public void specificRequest(){Console.WriteLine(适配者中的业务代码被调用);}}//对象适配器类class ObjectAdapter : ITarget{private Adaptee adaptee;public ObjectAdapter(Adaptee adaptee){this.adaptee adaptee;}public void request(){adaptee.specificRequest();}}//客户端代码class Program{public static void Main(String[] args){Console.WriteLine(对象适配器模式测试);Adaptee adaptee new Adaptee();ITarget target new ObjectAdapter(adaptee);target.request();}}
}